Note that there are some explanatory texts on larger screens.

plurals
  1. POIterating over items in a search result view with Django haystack MultiValueField
    primarykey
    data
    text
    <p>If I have a MultiValueField on one of my search indexes, and I want to display each value in the search results, how would I do that? It seems like something is not being formatted appropriately or I am somehow misunderstanding the MultiValueField?</p> <pre><code>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 </code></pre> <p>In leveraging this in my template view:</p> <pre><code> {% if result.attrs|length %} &lt;div class="attributes"&gt; &lt;ul&gt; {% for a in result.attrs %} &lt;li class="{% cycle "clear" "" "" %}"&gt;&lt;span class="name"&gt;{{ a.name }}&lt;/span&gt;: &lt;span class="value"&gt;{{ a.value }}&lt;/span&gt;&lt;/li&gt; {% endfor %} &lt;/ul&gt; &lt;div class="clear"&gt;&lt;/div&gt; &lt;/div&gt; {% endif %} </code></pre> <p>This seems to return nothing for me :(</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload