0
ブログの投稿があります。検索インデックスにコメントを追加しようとしていますが、何らかの理由で動作しません。 私のコメントには、問題。 ところで、それはタグのために働く。 Haystack - 準備されたフィールドを持つオブジェクトが見つかりません
class BlogIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
headline = indexes.CharField(model_attr="headline", null=True)
body = indexes.CharField(model_attr="body")
date = indexes.DateTimeField(model_attr='date')
mytags = indexes.MultiValueField()
mycomments = indexes.MultiValueField()
def prepare_mytags(self, obj):
return [tag.name for tag in obj.tags.all()]
def prepare_mycomments(self, obj):
cur_model = ContentType.objects.get_for_model(obj)
cur_comments = Comment.objects.filter(content_type=cur_model, object_pk=obj.id)
return [str(cur_c.content) for cur_c in cur_comments]
def get_model(self):
return Snip
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects.filter(date__lte=timezone.now())
この
は私のテンプレートです:それは重要な場合{{ object.headline }}
{{ object.body }}
{% for c in object.mycomments %}{{ c }} {% endfor %}
{% for tag in object.tags.all %}{{ tag.name }} {% endfor %}
私もDRF-干し草を使用していますが、これはシリアライザです:
class BlogSearchSerializer(HaystackSerializer):
class Meta:
index_classes = [BlogIndex]
fields = [
"headline", "body", "date", "mytags", 'mycomments', "content",
]
私が探していますcontent = XXコメントには何も見つかりません。
私は間違っていますか?
おかげ R
問題は、コメントの外部キーが一般的な外部キーであるため、私はcomment_set AFAKを使用できません。ありがとう! – Rani
私はそれを固定しましたが、完全にはそうしませんでした。ブログモデルに 'comments = GenericRelation(Comment)'を追加しました。これでインデックスを作成して/?content = XXを使って検索できますが、コメントだけを検索することはできず、結果にコメントを表示できませんdrf-haystackを使用します。 – Rani
'Blog'モデルにメソッドを追加することができます。たとえば、現在' BlogIndex.prepare_mycomments'にあるロジックを実行し、一連のコメントを返す 'get_comments'メソッドです。次に、 'object.get_comments'を使ってテンプレート内のテンプレートにアクセスできます。 – solarissmoke