-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Unique together validation fails with default value #7489
Comments
I'm not sure, but does this fix or help? #8002 Currently on master, the default value for a model field doesn't get copied to the field on the serializer, so how could it do the appropriate validation? |
I looked into it, and the If you modify your test and remove the I think it has to do with the fact that the validators are passed only the If you look at |
Thanks for following up @john-parton - it seems the test case is still failing when run against your branch
|
Yeah, see my previous comment from 3 minutes ago. I have a patch that fixes, I think, but it's a little finicky. I'll see if I can get it stable and submit a PR. |
Here it is: #8004 This might cause some breakage, because previously there could be a dirty value in the database, and so long as you didn't try to touch it, it wouldn't cause re-validation. For instance, suppose you have a validator on some_bad_field that prohibits "bad_value", and another unrelated field 'foo'. # OK
instance = MyModel.objects.create(some_bad_field='bad_value')
# Old way didn't complain
serializer = MyModelSerializer(instance=instance, data={'foo': 'bar'})
# New way will re-validate ALL the fields, including some_bad_field, and fail
serializer = MyModelSerializer(instance=instance, data={'foo': 'bar'}) Whether that's desirable behavior or not should probably be discussed. |
Certainly fixes my issue, thanks! I started poking around at alternatives, but it seems to fix this elsewhere would require some pretty invasive changes. Perhaps this change would be more palatable if it were configurable via something like
|
Ah yeah, making it opt-in makes sense. Probably opting in at the SETTINGS level would make a little more sense? Having to remember to do this for each model seems like a pain point. You probably want all your serializes to have consistent behavior one way or the other. I'll see if I can update my PR |
@tomchuk I updated my PR so that the behavior is opt-in, using either a setting You might want to go weigh in there. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I think this is still current. |
I've faced this recenty, would like to go all in to figure out what's causing this and hopefully fix... Can I take this up? |
this is what is remaining #8130 (review) |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Are you still in? |
Checklist
master
branch of Django REST framework.Steps to reproduce
See test case in PR
Expected behavior
Serializer validates
Actual behavior
Serializer does not validate
I haven't dug into the internals too much, but it appears what happens is that when validating the unique_together constraint on a model where one of the fields has a default value that is not passed in as data to the serializer, the default value is substituted for the purposes of validation, even in the case where that field is available from an instance.
The text was updated successfully, but these errors were encountered: