Search code examples
pythondjangodjango-rest-frameworkswaggermongoengine

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

model :

from mongoengine import *

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

serializer:

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

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

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

@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

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

enter image description here

anyone can help me to solve this problem


Solution

  • 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()