Search code examples
djangoelasticsearchdjango-rest-frameworkelasticsearch-dslelasticsearch-dsl-py

Django Elastic Search: AttributeError: type object 'PostDocument' has no attribute 'Django'


I am very new in elasetic search in django... When i run this command, python3 manage.py search_index --rebuild it fires me this error: I am not getting whats wrong with it

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'

This is my 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',
        ]

and this is my models:

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)

I am not getting whats wrong with my code, even it is not firing me my code problem, it is firing me some odd error..


Solution

  • It's not Meta class, it should be Django class which you should create and tell model, fields and other config related to this document and it's model. Please see documentation and it's sample example. 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',
            ]