Nếu tôi có một MultiValueField trên một trong các chỉ mục tìm kiếm của mình và tôi muốn hiển thị từng giá trị trong kết quả tìm kiếm, tôi sẽ làm như thế nào? Nó có vẻ như một cái gì đó không được định dạng thích hợp hoặc tôi bằng cách nào đó hiểu lầm MultiValueField?Lặp lại các mục trong giao diện kết quả tìm kiếm với Django haystack 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
Trong tận dụng này trong mẫu của tôi xem:
{% 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 %}
Điều này dường như trở gì cho tôi :(