4

如何通过 django-haystack字段将json存储在 elasticsearch 中?

目前我正在使用以下技巧:

# search_indexes.py

class BlogPostIndex(indexes.SearchIndex, indexes.Indexable):
    author = indexes.CharField()

    @staticmethod
    def prepare_author(obj):
        return json.dumps(serializers.UserMinimalSerializer(obj.author).data)
# serializers.py

class BlogPostSearchSerializer(HaystackSerializer):
    author = serializers.SerializerMethodField()

    @staticmethod
    def get_author(obj):
        return json.loads(obj.author)
4

0 回答 0