ChoiceField should support a child just like the ListField #8358
Unanswered
michal-klimek
asked this question in
Ideas & Suggestions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Dear people,
I have a custom Django field
LanguageFieldthat works with a data classLanguagewhich itself is a container for valid BCP 47 language tags. TheLanguagehas other properties specific to the language tag domain. I have also aLanguageFieldthat subclassesserializers.Fieldand implements all required serialization/deserialization logic. As the django field indirectly subclasses themodels.CharFieldI can usechoicesargument, and thus I expect DRF to handle it accordingly. I useModelSerializer.serializer_field_mappingto let DRF know how to deal with my customLanguageFieldautomatically without the need of declaring it explicitly in my serializers.However it does not work along with the
choicesoption. This is because theChoiceFieldthrows away the information about the source model field and it's possible requirements. If the problem was only in theChoiceFieldI wouldn't bother, because I could just replace theChoiceFieldclass by my own one. But the real problem lies in the code that transforms the origin field into theChoiceFieldand strips away information not supported by theChoiceField.The following link points to a place with the issue code:
django-rest-framework/rest_framework/serializers.py
Line 1238 in 5bea22f
I suggest to refactor the
ChoiceFieldin a similar way theListFieldworks, by adding achildfeature. This wayChoiceFieldmay honor all child-specific requirements and may act as a proxy that uses serialization/deserialization from the child field.Currently the whole serialization fails, throwing an exception, because a
Languageobject is directly passed to theJSONserializer which simply can not handle it.Beta Was this translation helpful? Give feedback.
All reactions