2010-10-22 7 views
6

Jeśli mam MultiValueField w jednym z moich indeksów wyszukiwania i chcę wyświetlić każdą wartość w wynikach wyszukiwania, w jaki sposób mogę to zrobić? Wygląda na to, że coś nie jest odpowiednio sformatowane lub w jakiś sposób nie rozumiem MultiValueField?Powtórzenie pozycji w widoku wyników wyszukiwania za pomocą sterty Django MultiValueField

class PageAttachmentIndex(indexes.SearchIndex): 
    # This should reference search/indexes/pages/pageattachment_text.txt 
    text  = indexes.CharField(document=True, use_template=True) 
    title  = indexes.CharField(model_attr='name') 
    page  = indexes.IntegerField(model_attr='page_id') 
    attrs  = indexes.MultiValueField() 
    file  = indexes.CharField(model_attr='file') 
    filesize = indexes.IntegerField(model_attr='file__size') 
    timestamp = indexes.DateTimeField(model_attr='timestamp') 
    url  = indexes.CharField(model_attr='page') 

    def prepare_attrs(self, obj): 
     """ Prepare the attributes for any file attachments on the 
      current page as specified in the M2M relationship. """ 
     # Add in attributes (assuming there's a M2M relationship to 
     # attachment attributes on the model.) Note that this will NOT 
     # get picked up by the automatic schema tools provided by haystack 
     attributes = obj.attributes.all() 
     return attributes 

dźwigni w moim szablonu widzenia:

{% if result.attrs|length %} 
    <div class="attributes"> 
     <ul> 
     {% for a in result.attrs %} 
      <li class="{% cycle "clear" "" "" %}"><span class="name">{{ a.name }}</span>: <span class="value">{{ a.value }}</span></li> 
     {% endfor %} 
     </ul> 
     <div class="clear"></div> 
    </div> 
    {% endif %} 

Wydaje się powrócić nic dla mnie :(

Odpowiedz

1

Rzeczywisty problem jest, że pole M2M nie jest indeksowana w searchengine You. powinien zwracać obiekty pierwotne (list, string, int, itp.) w funkcji prepare_, a nie Django Moldel.

np.


def prepare_attr(self, obj): 
    return [str(v) for v in obj.attrs.all()] 
Powiązane problemy