Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix django-mongoengine support #974

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions drf_spectacular/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,14 @@ def _get_pagination_parameters(self):
return paginator.get_schema_operation_parameters(self.view)

def _map_model_field(self, model_field, direction):
assert isinstance(model_field, models.Field)

# Special case: SlugRelatedField also allows to point to a callable @property.
if callable(model_field):
return self._map_response_type_hint(model_field)
if not isinstance(model_field, models.Field):
field_name = getattr(model_field, "name", model_field)
raise AssertionError(f'Field "{field_name}" must point to either a property or a model field.')

# to get a fully initialized serializer field we use DRF's own init logic
try:
field_cls, field_kwargs = serializers.ModelSerializer().build_field(
Expand Down Expand Up @@ -698,13 +705,7 @@ def _map_serializer_field(self, field, direction, bypass_extensions=False):
# be graceful and default to string.
model_field = follow_field_source(model, source, default=models.TextField())

# Special case: SlugRelatedField also allows to point to a callable @property.
if callable(model_field):
schema = self._map_response_type_hint(model_field)
elif isinstance(model_field, models.Field):
schema = self._map_model_field(model_field, direction)
else:
assert False, f'Field "{field.field_name}" must point to either a property or a model field.'
schema = self._map_model_field(model_field, direction)

# primary keys are usually non-editable (readOnly=True) and map_model_field correctly
# signals that attribute. however this does not apply in the context of relations.
Expand Down