Skip to content

ValueError: Cannot use ModelSerializer with Abstract Models when generating schema #1422

@maycuatroi1

Description

@maycuatroi1

Describe the bug
When accessing /api/schema/ endpoint after migrating from drf-yasg to drf-spectacular, the schema generation fails with a ValueError: Cannot use ModelSerializer with Abstract Models. The error occurs during schema generation when drf-spectacular tries to map serializers.

To Reproduce

# The error happens when drf-spectacular tries to generate schema for viewsets that use abstract models
# Stack trace shows the issue occurs in:
# File "...\drf_spectacular\openapi.py", line 1042, in _map_basic_serializer
#     for field in serializer.fields.values():
# File "...\rest_framework\serializers.py", line 1067, in get_fields
#     raise ValueError("Cannot use ModelSerializer with Abstract Models.")

The error is triggered when accessing:

GET /api/schema/ HTTP/1.1

Expected behavior
The schema should be generated successfully without errors. Abstract models should either be skipped or handled appropriately during schema generation.

Additional context

  • Recently migrated from drf-yasg to drf-spectacular
  • Multiple warnings about WorkflowServiceAuthentication not having an OpenApiAuthenticationExtension
  • Warnings about unable to resolve type hints for several serializer methods
  • The full stack trace shows the error originates from DiscussionActivityViewSet and related activity viewsets

Environment:

  • drf-spectacular version: [latest from migration]
  • Django version: [from traceback appears to be recent]
  • Django REST Framework version: [from traceback appears to be recent]
  • Python version: [from path appears to be Python 3.x on Windows]

Full Error Log:

[11/Jun/2025 01:10:45] "GET /api/schema/ HTTP/1.1" 500 188110
C:\github\evo-lms-workspace\evo-lms-django\elearning\views\activity_viewsets\assessment_viewset.py: Warning [DiscussionActivityViewSet > AbstractActivityDetailSerializer > AssessmentActivityViewSet]: could not resolve authenticator <class 'common.authentication.WorkflowServiceAuthentication'>. There was no OpenApiAuthenticationExtension registered for that class. Try creating one by subclassing it. Ignoring for now.
...
Internal Server Error: /api/schema/
Traceback (most recent call last):
  ...
  File "C:\Users\somet\miniconda3\Lib\site-packages\rest_framework\serializers.py", line 1067, in get_fields
    raise ValueError(
ValueError: Cannot use ModelSerializer with Abstract Models.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions