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 : ] (

from mongoengine import *

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

[that is serializer :] (

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] (

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')

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] (

"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