AttributeError: 'QuerySet' object has no attribute 'model'

I want to add documentation for the Django app

  1. I use rest_framework_mongoengine, rest_framework
  2. OpenAPI 3.0, drf-spectacular swagger

[that is model : ] (https://i.stack.imgur.com/49ynm.png)

from mongoengine import *

class Service(Document):
    student_id = StringField(required=True)
    name = StringField(max_length=50)
    age = IntField()

[that is serializer :] (https://i.stack.imgur.com/kfgTd.png)

from Service.models import Service
from rest_framework_mongoengine import serializers as mongoserializers

class ServiceSerializer(mongoserializers.DocumentSerializer):
    class Meta:
        model = Service
        fields = '__all__'

[that is views] (https://i.stack.imgur.com/5ehJL.png)

import mongoengine
from .models import Service
from rest_framework import generics
from .serializers import ServiceSerializer
from .docs import list_service, create_service, update_service, delete_service
from rest_framework_mongoengine.viewsets import ModelViewSet as MongoModelViewSet


mongoengine.connect(db='Ecommerce', host='localhost:27017')

@create_service()
class CreateServiceAPI(generics.CreateAPIView):
    queryset = Service.objects.all()
    serializer_class = ServiceSerializer

I created documentation but when I execute any endpoint in documentation this error happens [documentation] (https://i.stack.imgur.com/PqdeR.png)

"AttributeError: 'QuerySet' object has no attribute 'model'"

anyone can help me to solve this problem

Instead of importing generics.CreateAPIView from rest_framework, you should import from rest_framework_mongoengine, like this:

from rest_framework_mongoengine import generics

change model to add fields:

from django_mongoengine import Document, fields

class Service(Document):
    student_id = fields.StringField(blank=True)
    name = fields.StringField(max_length=50)
    age = fields.IntField()

Back to Top