2 django django-taggit algolia
我在使用Algolia Django与我的一个包含TaggitManager()字段的模型集成时遇到了一些问题.我正在运行此命令时抛出以下错误:
$ python manage.py algolia_reindex
AttributeError: '_TaggableManager' object has no attribute 'name'
Run Code Online (Sandbox Code Playgroud)
我已经看过Taggit文档,但我不确定如何将Algolia搜索索引方法概述的方法结合起来.
index.py:
import django
django.setup()
from algoliasearch_django import AlgoliaIndex
class BlogPostIndex(AlgoliaIndex):
fields = ('title')
settings = {'searchableAttributes': ['title']}
index_name = 'blog_post_index'
Run Code Online (Sandbox Code Playgroud)
models.py:
from taggit.managers import TaggableManager
class Post(models.Model):
...some model fields...
tags = TaggableManager()
Run Code Online (Sandbox Code Playgroud)
要taggit使用Post字段索引标记,您需要公开一个callable,它将Blog Post的标记作为字符串列表返回.
最好的选择是将它们存储为_tags,这样可以在查询时过滤标记.
你PostIndex会看起来像这样:
class PostIndex(AlgoliaIndex):
fields = ('title', '_tags')
settings = {'searchableAttributes': ['title']}
index_name = 'Blog Posts Index'
should_index = 'is_published'
Run Code Online (Sandbox Code Playgroud)
至于Post:
class Post(models.Model):
# ...some model fields...
tags = TaggableManager()
def _tags(self):
return [t.name for t in self.tags.all()]
Run Code Online (Sandbox Code Playgroud)
按照这些说明,您的记录将使用各自的标记编制索引:
您可以查看我们的Django演示taggit分支,它演示了这些步骤.
| 归档时间: |
|
| 查看次数: |
293 次 |
| 最近记录: |