Django Elastic Search:AttributeError:类型对象'PostDocument'没有属性'Django'

时间:2019-06-29 17:39:49

标签: django elasticsearch django-rest-framework elasticsearch-dsl elasticsearch-dsl-py

我在Django的弹性搜索中非常新... 当我运行此命令时,python3 manage.py search_index --rebuild  它触发了我这个错误:我没有弄错什么

File "/home/pyking/.local/lib/python3.6/site-packages/django_elasticsearch_dsl/registries.py", line 39, in register_document
    django_meta = getattr(document, 'Django')
AttributeError: type object 'PostDocument' has no attribute 'Django'

这是我的documents.py

from django_elasticsearch_dsl import DocType, Index
from blog2.models import Article

posts = Index('articles')

@posts.doc_type
class PostDocument(DocType):
    class Meta:
        model = Article

        fields = [
            'alias',
            'author',
            'title',
            'body',
            'category',
        ]

这是我的模特

class Article(models.Model):
    alias = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name='author')
    title = models.CharField(max_length=200)
    body = models.TextField()
    category = models.ForeignKey(Category, on_delete=models.CASCADE)

我没有发现我的代码有什么问题,即使它没有激发我的代码问题,也激发了我一些奇怪的错误。

1 个答案:

答案 0 :(得分:1)

这不是Meta类,应该是Django类,您应该创建并告诉模型,字段以及与此文档及其模型相关的其他配置。 请参阅文档及其示例示例。 https://github.com/sabricot/django-elasticsearch-dsl#quickstart

from django_elasticsearch_dsl import Document, Index
from django_elasticsearch_dsl.registries import registry
from blog2.models import Article

posts = Index('articles')

@registry.register_document
@posts.document
class PostDocument(Document):
    class Django:
        model = Article

        fields = [
            'alias',
            'author',
            'title',
            'body',
            'category',
        ]