2013-06-03 7 views
7

आप कैसे एक अमूर्त खोज इंडेक्स क्लास बनाते हैं, इसी तरह Django आपको अमूर्त आधार मॉडल कैसे देता है?एक अमूर्त Haystack SearchIndex क्लास बनाने के लिए कैसे करें

मेरे पास कई खोज इंडेक्स हैं जो मैं एक ही मूल फ़ील्ड (object_id, timestamp, महत्व, आदि) देना चाहता हूं। वर्तमान में, मैं इस कोड को डुप्लिकेट कर रहा हूं, इसलिए मैं "बेसइंडेक्स" बनाने की कोशिश कर रहा हूं और बस सभी वास्तविक इंडेक्स क्लास इस से प्राप्त होते हैं।

मैंने कोशिश कर रहा हूँ:

class BaseIndex(indexes.SearchIndex, indexes.Indexable): 
    text = indexes.CharField(document=True, use_template=True) 
    object_id = indexes.IntegerField() 
    timestamp = indexes.DateTimeField() 

    class Meta: 
     abstract = True 

class PersonIndex(BaseIndex): 
    ...other fields... 

लेकिन यह मुझे त्रुटि देता है:

NotImplementedError: You must provide a 'model' method for the '<myapp.search_indexes.BaseIndex object at 0x18a7328>' index. 

तो मैं तो कोशिश की:

class BaseIndex(object): 
    text = indexes.CharField(document=True, use_template=True) 
    object_id = indexes.IntegerField() 
    timestamp = indexes.DateTimeField() 

class PersonIndex(BaseIndex, indexes.SearchIndex, indexes.Indexable): 
    first_name = indexes.CharField() 
    middle_name = indexes.CharField() 
    last_name = indexes.CharField() 

लेकिन इन मुझे त्रुटि देता है:

SearchFieldError: The index 'PersonIndex' must have one (and only one) SearchField with document=True. 

मैं एक कस्टम खोज इंडेक्स उपclass से कैसे प्राप्त करूं?

+0

एक [खोज अनुक्रमणिका 'Meta' वर्ग] (http://django-haystack.readthedocs.org/en/latest/searchindex_api.html#modelsearchindex) एक विशेषता नहीं है' abstract' ... पता नहीं तुम कहाँ से मिला? –

उत्तर

13

बस किसी भी चीज पर माता-पिता के रूप में indexes.Indexable शामिल न करें जो आप अनुक्रमणित नहीं करना चाहते हैं।

तो अपना पहला उदाहरण संशोधित करना।

class BaseIndex(indexes.SearchIndex): 
    text = indexes.CharField(document=True, use_template=True) 
    object_id = indexes.IntegerField() 
    timestamp = indexes.DateTimeField() 

    class Meta: 
     abstract = True 

class PersonIndex(BaseIndex, indexes.Indexable): 
    ...other fields... 
+0

यह मेरे लिए काम किया। मुझे विश्वास नहीं है कि आपको काम करने के लिए 'वर्ग मेटा: अमूर्त = सही' बिट की आवश्यकता है। – Esteban

+0

'abstract = True' उपयोगी है यदि आप अन्य वर्गों को सीधे अपनी कक्षा का उपयोग करने की कोशिश करना बंद करना चाहते हैं। –

+1

वास्तव में, यह डीजेंगो मॉडल के लिए है, इंडेक्स के लिए यह काम नहीं कर रहा है, शायद मैंने इसे सिर्फ प्रश्न से कॉपी किया है। –

4
class BaseIndex(indexes.SearchIndex): 
    model=None   

    text = indexes.CharField(document=True, use_template=True) 
    object_id = indexes.IntegerField() 
    timestamp = indexes.DateTimeField() 

    def get_model(self): 
     return self.model 

class PersonIndex(BaseIndex, indexes.Indexable): 
    first_name = indexes.CharField() 
    middle_name = indexes.CharField() 
    last_name = indexes.CharField() 

    def get_model(self): 
     return Person 
संबंधित मुद्दे