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
-
Dear people,
I have a custom Django field
LanguageField
that works with a data classLanguage
which itself is a container for valid BCP 47 language tags. TheLanguage
has other properties specific to the language tag domain. I have also aLanguageField
that subclassesserializers.Field
and implements all required serialization/deserialization logic. As the django field indirectly subclasses themodels.CharField
I can usechoices
argument, and thus I expect DRF to handle it accordingly. I useModelSerializer.serializer_field_mapping
to let DRF know how to deal with my customLanguageField
automatically without the need of declaring it explicitly in my serializers.However it does not work along with the
choices
option. This is because theChoiceField
throws away the information about the source model field and it's possible requirements. If the problem was only in theChoiceField
I wouldn't bother, because I could just replace theChoiceField
class by my own one. But the real problem lies in the code that transforms the origin field into theChoiceField
and 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
ChoiceField
in a similar way theListField
works, by adding achild
feature. This wayChoiceField
may 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
Language
object is directly passed to theJSON
serializer which simply can not handle it.Beta Was this translation helpful? Give feedback.
All reactions