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

Propagate 'default' from model_field to serializer field. #9030

Merged
merged 10 commits into from
Aug 7, 2023

Conversation

rnshaikh
Copy link
Contributor

@rnshaikh rnshaikh commented Jul 1, 2023

This is PR for changes requested in PR #8130.
default fields propagated from model to serializers.

  1. Generate 'default' for serializer as generated for openapi schema.
  2. Updated OPTIONS response to accommodate default field there. created Testcase to check default is generated for OPTIONS response for different field.

fixes #7469

john-parton and others added 2 commits June 24, 2023 16:07
Fix encode#7469.

Co-authored-by: Nikhil Benesch <nikhil.benesch@gmail.com>
@auvipy auvipy self-requested a review July 2, 2023 07:42
@auvipy auvipy added this to the 3.15 milestone Jul 2, 2023
@auvipy
Copy link
Member

auvipy commented Jul 2, 2023

I think we should also document the intended side effect for this change

@rnshaikh
Copy link
Contributor Author

rnshaikh commented Jul 8, 2023

@auvipy Okay. should i create list of side effects?

@auvipy
Copy link
Member

auvipy commented Jul 10, 2023

that would be great

@auvipy
Copy link
Member

auvipy commented Jul 26, 2023

Hey Rizwan! would be great if you could revisit this and add some docs on the intended side effects topic here

@auvipy
Copy link
Member

auvipy commented Jul 26, 2023

this is the only remaining issue to get merged for 3.15 release https://github.com/encode/django-rest-framework/milestones/3.15

@rnshaikh
Copy link
Contributor Author

@auvipy apologize for delay.
I have gone through the effects of these changes. find following points.

  1. all default value of model will be pass as default to serializer and to the options method.
  2. if the default is callable then it is not propagated in options.
  3. if the default is callable is not propagated but value will be evaluated each time it is used.
  4. if the value is not given then default value will be present in serializer also avaliable in serializer's methods.
  5. if the value is not given specified validation on given field will be evaluted on default value as that field will be present in serializer.

@auvipy
Copy link
Member

auvipy commented Jul 30, 2023

can you please add them as a note in the relevant doc section?

@auvipy auvipy requested a review from a team July 31, 2023 03:53
@rnshaikh
Copy link
Contributor Author

rnshaikh commented Jul 31, 2023

@auvipy I have added notes regarding default value propagation from model to serializer in default argument section of serializer field

docs/api-guide/fields.md Outdated Show resolved Hide resolved
docs/api-guide/fields.md Outdated Show resolved Hide resolved
@auvipy auvipy requested a review from a team August 2, 2023 05:32
docs/api-guide/fields.md Outdated Show resolved Hide resolved
docs/api-guide/fields.md Outdated Show resolved Hide resolved
docs/api-guide/fields.md Outdated Show resolved Hide resolved
docs/api-guide/fields.md Outdated Show resolved Hide resolved
docs/api-guide/fields.md Outdated Show resolved Hide resolved
@auvipy auvipy merged commit 7a9db57 into encode:master Aug 7, 2023
6 of 7 checks passed
kernicPanel added a commit to openfun/joanie that referenced this pull request Mar 18, 2024
DRF verson 3.15 propagate 'default' from model field to serializer.
encode/django-rest-framework#9030
kernicPanel added a commit to openfun/joanie that referenced this pull request Mar 18, 2024
DRF verson 3.15 propagate 'default' from model field to serializer.
encode/django-rest-framework#9030
kernicPanel added a commit to openfun/joanie that referenced this pull request Mar 18, 2024
DRF verson 3.15 propagate 'default' from model field to serializer.
encode/django-rest-framework#9030
kernicPanel added a commit to openfun/joanie that referenced this pull request Mar 18, 2024
DRF verson 3.15 propagate 'default' from model field to serializer.
encode/django-rest-framework#9030
kernicPanel added a commit to openfun/joanie that referenced this pull request Mar 18, 2024
DRF verson 3.15 propagate 'default' from model field to serializer.
encode/django-rest-framework#9030
tomchristie added a commit that referenced this pull request Mar 21, 2024
auvipy pushed a commit that referenced this pull request Mar 22, 2024
* Revert #9030

* Fix test case
@vignirvignir
Copy link

Hi!

After upgrading from DRF 3.14 to 3.15 our tests broke when a model serializer that allows required=False for a non-nullable field in the corresponding model is now ignored so the endpoint that uses the serializer to create the object throws an error claiming you need to provide the value for the field.

However, the value that eventually goes into the field of the model might be managed in the create method of the serializer and set there.

In this example there is an entity_id field that refers to an entity field on the model. The entity_id does not need to be provided by the user as other information are used to create the Entity objects and assign to the entity field of the Customer model in the create method of the serializer.

class CustomerDetailSerializer(serializers.ModelSerializer):
    entity_id = EntityForeignKey(source="entity", write_only=True, allow_null=True, required=False)


class Customer(models.Model):
    entity = models.ForeignKey("users.Entity", related_name="customers", on_delete=models.PROTECT)

kernicPanel added a commit to openfun/joanie that referenced this pull request Mar 25, 2024
As DRF encode/django-rest-framework#9030 has
been reverted in 3.15.1, the related hack is no longer needed.
kernicPanel added a commit to openfun/joanie that referenced this pull request Mar 25, 2024
As DRF encode/django-rest-framework#9030 has
been reverted in 3.15.1, the generated openAPI schema is missing default
values, which might be revelant in the documentation.
kernicPanel added a commit to openfun/joanie that referenced this pull request Mar 25, 2024
As DRF encode/django-rest-framework#9030 has
been reverted in 3.15.1, the related hack is no longer needed.
kernicPanel added a commit to openfun/joanie that referenced this pull request Mar 25, 2024
As DRF encode/django-rest-framework#9030 has
been reverted in 3.15.1, the generated openAPI schema is missing default
values, which might be revelant in the documentation.
kernicPanel added a commit to openfun/joanie that referenced this pull request Mar 26, 2024
As DRF encode/django-rest-framework#9030 has
been reverted in 3.15.1, the related hack is no longer needed.
kernicPanel added a commit to openfun/joanie that referenced this pull request Mar 26, 2024
As DRF encode/django-rest-framework#9030 has
been reverted in 3.15.1, the generated openAPI schema is missing default
values, which might be revelant in the documentation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ModelSerializer generate ChoicesField will discard default parameter
5 participants