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

Not possible to override serializer_field_mappings with a field that has choices (for example a from ChoiceField derived field) #9179

Open
gabn88 opened this issue Dec 4, 2023 · 3 comments

Comments

@gabn88
Copy link
Contributor

gabn88 commented Dec 4, 2023

Because of this line:

https://github.com/encode/django-rest-framework/blame/0abb84fa3955956fc6156cc2c51222cd4ceae783/rest_framework/serializers.py#L1286

If you set a serializer_field_mapping to for example a fields.CountryField: serializer_fields.CountryField (from django_countries), it will be replaced by a 'dumb' ChoiceField and the serialization will fail. This makes that you have to manually set this field on every serializer and the serializer_field_mapping mechanism won't work.

Another example is the TimeZoneField (if you use the extended version that shows the choices via the OPTIONS request).

@gabn88
Copy link
Contributor Author

gabn88 commented Dec 5, 2023

My suggestion is to change

field_class = self.serializer_choice_field

to:

if not issubclass(field_class, self.serializer_choice_field):
            field_class = self.serializer_choice_field

That way you enable developers to override the serializer_field_mapping also for ChoiceFields (now this is impossible).

@Pavankumardontha
Copy link

@gabn88 can i start contributing to this ?

@gabn88
Copy link
Contributor Author

gabn88 commented Dec 14, 2023

@Pavankumardontha that would be great.

I had to change the custom ChoiceFields (CountryField / TimezoneField) also a little, because they need the argument 'choices' and other arguments (for example 'countries') won't be passed along. Something to keep in mind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants