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: let use a default value for foreignkey model field #901

Merged
merged 1 commit into from
Mar 4, 2023
Merged
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
9 changes: 8 additions & 1 deletion drf_spectacular/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,14 @@ def _get_serializer_field_meta(self, field, direction):
if not field.read_only:
meta['minLength'] = 1
if field.default is not None and field.default != empty and not callable(field.default):
if isinstance(field, (serializers.ModelField, serializers.SerializerMethodField)):
if isinstance(
field,
(
serializers.ModelField,
serializers.SerializerMethodField,
serializers.PrimaryKeyRelatedField,
),
):
# Skip coercion for lack of a better solution. ModelField.to_representation()
# and SerializerMethodField.to_representation() are special in that they require
# a model instance or object (which we don't have) instead of a plain value.
Expand Down