-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
UniqueTogetherValidator is incompatible with field source #7003
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
Comments
Hi @anveshagarwal. After looking at your PR, I've updated the issue description. Note that the bug is not limited to read-only fields, however the writeable and read-only cases raise different errors. |
@rpkilby So ideally uniquetogether validator in serializer should take the name of the field declared in serializer, not the name of the source? But till now it was taking the name of the source or field in the model. |
Yes, per the
|
Thanks for sharing the docs. |
Uh oh!
There was an error while loading. Please reload this page.
The
UniqueTogetherValidator
is incompatible with serializer fields where the serializer field name is different than the underlying model field name. i.e., fields that have asource
specified.This manifests itself in two ways:
required
validation error.django.core.exceptions.FieldError
.Example setup
Test output
Original Description
Checklist
master
branch of Django REST framework.Steps to reproduce
according to the changes in drf 3.8.2 the read_only+default fields should be explicilty saved in perform_create of viewset or create, save or update method of serializer.
this perform_create is called after validating the data by
serializer.is_valid()
inside create method of CreateModelMixinnow in my codebase i had two cases:
case1 : when the name of field of serializer is same as model like:
model:
serializer:
In this case serializer.is_valid() is not failing if i POST something to this serializer and my overwritten perform_create is being called so problem here
because as we can see these fields are included in _read_only_defaults after #5922 and here https://github.com/encode/django-rest-framework/blob/master/rest_framework/serializers.py#L451 the field.field_name is added in the return value that will be validated with uniquetogether so no issues come.
case 2: when the name of field of serializer is 'not' same as model's field and a source is added in the serializer field like:
model:
serializer:
in this case, if i POST something to this serializer, a ValidationError exception is thrown
saying
{'a': [ErrorDetail(string='This field is required.', code='required')]}
because since field.field_name is included in the return ordered dictionary as key so here in the unique together will fail as it expects ('a', 'c') but gets ('b', 'c').
Expected behavior
in second case also it should work fine
Actual behavior
throwing error
{'a': [ErrorDetail(string='This field is required.', code='required')]}
Possible source of error found
Since all the fields (including read_only) were earlier present in _wriable_fields, this is the place where it used to get the field name from source - https://github.com/encode/django-rest-framework/blob/master/rest_framework/serializers.py#L496
And since it started using the method _read_only_defaults for these read_only+default fields, it populates using field name rather than source name - https://github.com/encode/django-rest-framework/blob/master/rest_framework/serializers.py#L451
which maybe a bug
The text was updated successfully, but these errors were encountered: