Propagate 'default' from model_field to serializer field
Description
This is a resubmission of #8002 that will hopefully pass tests. The tl;dr is that field defaults are now propagated from models to model serializers.
Fix #7469.
I'll close my issue.
Just a note for discussion, part of the motivating factor for this change was to include the default value in the OPTIONS response. More info here: https://github.com/encode/django-rest-framework/discussions/8003
If this PR gets merged, I can rework 8003 as its own PR.
Hi folks. 👋
So, if we did want to accept this, then I'd suggest that this pull request should also update the OPTIONS responses in order to include the default - https://github.com/encode/django-rest-framework/blob/master/rest_framework/metadata.py#L115
Staying in line with how we generate 'default' for schemas would make sense... https://github.com/encode/django-rest-framework/blob/6ea95b6ad1bc0d4a4234a267b1ba32701878c6bb/rest_framework/schemas/openapi.py#L535-L536
I'm a bit reluctant, simply because at this point in it's lifecycle almost any change in REST framework ends up having unintended side-effects for existing users, and I could easily see this being the case for this change. We've used "default on the model => not required on the serializer" since ~forever.
On the other hand, exposing default values on the schema and OPTIONS responses does seem like an improvement.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
I think this may also fix #7489 which is still current.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Anyone should feel free to take over this PR. I’m not going to have time myself.
@ashupednekar Do you have time for this?
(Asking due to https://github.com/encode/django-rest-framework/issues/7489#issuecomment-1556159800)
@samims you can also check this out
@auvipy I have made the changes requested in this PR. default fields propagated from model to serializers.
- Generate 'default' for serializer as generated for open API schema.
- Updated OPTIONS response to accommodate the default field there. Created Testcase to check default is generated for OPTIONS response for different fields.
I have created PR for this #9030 I can't update this PR as I don't have permission.