6

検索インデックスの1つにMultiValueFieldがあり、検索結果に各値を表示したい場合、どうすればよいですか?何かが適切にフォーマットされていないようです、または私はどういうわけか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

テンプレートビューでこれを活用するには:

    {% 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 %}

これは私には何も返さないようです:(

4

1 に答える 1

1

実際の問題は、M2M フィールドが SearchEngine でインデックス化されていないことです。Django Moldel インスタンスではなく、プリミティブ オブジェクト (リスト、文字列、int など) を prepare_ 関数で返す必要があります。

例えば


def prepare_attr(self, obj): 
  return [str(v) for v in obj.attrs.all()]
于 2011-08-14T19:02:40.567 に答える