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
When a serializer field defined explicitly, extra_kwargs for that field are ignored #6581
Comments
Added a little more description on the documentation |
I consider the addition in the documentation to be good enough to close the issue yet. |
Note: a solution would need to consider serializer inheritance. |
This would be very helpful to handle |
Having |
I will try to explain my current issue, I have a serializer with many long fields. I want to reuse this Serializer to just make them optional (required=False) or change only one or two parameters: class BaseSerializer(Serializer):
field_one = serializers.CharField(required=True, many=True, allow_empty=True, ...)
field_second = serializers.CharField(required=True, many=True, allow_empty=True, ...)
field_third = serializers.CharField(required=True, many=True, allow_empty=True, ...) If I have to redefine all the fields, then inheritance becomes almost useless. Also if finally I realize I want to change The |
And in less time it take to explain it, we'll see request for some weird use cases such as: class HaveFunSerializer(Serializer):
field_one = serializers.CharField(required=True, many=True, allow_empty=True, ...)
class Meta:
extra_kwargs = {'field_one': {'required': False}}
class BaseSerializer(Serializer):
field_one = serializers.CharField(required=True, many=True, allow_empty=True, ...)
class Serializer1(BaseSerializer):
class Meta:
extra_kwargs = {'field_one': {'required': False}}
class Serializer2(Serializer1):
class Meta:
extra_kwargs = {'field_one': {'required': True}} At which point half of the world will argue the HaveFunSerializer's field_one should be required and the other say it shouldn't. Also, should Serializer2's field_one be set to required because Serialize1 set it to False or should the Serializer's extra_kwargs be dismissed ? So many thing that makes me think it's a broken design. Your options on the table being to use Python's |
I understand the issue now. For me the
Which would give Starting from this assumption
I am not sure to understand, if you could give me a small example ? Alternatively I could change it in the |
Steps to reproduce
Define a field explicity on a serializer, than add extra_kwargs for that field in the Meta class
Expected behavior
Should not raise validation error when my_field is not supplied in the request body
Actual behavior
Raises validation error for missing my_field
I think this behavior is caused by this part in get_fields method.
If this is the expected behavior though, which could be taking possible clashing settings into consideration, I think the documentation should be updated stating that extra_kwargs will not be taken into account if the field is defined explicitly in the serializer.
The text was updated successfully, but these errors were encountered: