'CategoryDocument' object has no attribute 'children' in Django elasticsearch

I setup everything for implementing elastic search in Django rest framework. I launched the elastic server using bat file and it runs successfully in the background. Then when I call the api , it throws me above error.

class Category(models.Model):
    
    name = models.CharField(max_length=255)
    name = models.CharField(max_length=255)      
    description = models.TextField(blank=True)
    status = models.BooleanField(default=True)

My search.py

class PaginatedElasticSearchAPIView(APIView, LimitOffsetPagination):
    serializer_class = None
    document_class = None

    @abc.abstractmethod
    def generate_q_expression(self, query):
        """This method should be overridden
        and return a Q() expression."""

    def get(self, request, query):
        try:
            q = self.generate_q_expression(query)
            search = self.document_class.search().query(q)
            response = search.execute()

            print(f'Found {response.hits.total.value} hit(s) for query: "{query}"')

            results = self.paginate_queryset(response, request, view=self)
            serializer = self.serializer_class(results, many=True)
            return self.get_paginated_response(serializer.data)
        except Exception as e:
            return HttpResponse(e, status=500)


class CategorySearchView(PaginatedElasticSearchAPIView):
    serializer_class = CategorySerializers
    document_class = CategoryDocument    

    def generate_q_expression(self, query):
        return Q(
            "multi_match",
            query=query,
            fields=[
                "name",
            ],
            fuzziness="auto",
        )

My serializers. py

class CategorySerializer(serializers.ModelSerializer):
    
    class Meta:
        model = Subject
        fields = ["id", "name","description","status"]

My documents.py:

@registry.register_document
class CategoryDocument(Document):
    # name = TextField()
    class Index:
        name = "category"
        settings = {"number_of_shards": 1, "number_of_replicas": 0}

    class Django:
        model = Category
        fields = [
            "name","description"
        ]

My urls.py :

http://localhost:8000/elastic/search/Math/

I tried to call this api family but it shows the above error. Is this because I have some models which has a foreign key fields associated with the Category model?

Back to Top