Description
Checklist
- I have verified that that issue exists against the
master
branch of Django REST framework. - I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
- This is not a usage question. (Those should be directed to the discussion group instead.)
- This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
- I have reduced the issue to the simplest possible case.
- I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)
When using a custom ListSerializer to update data and providing the instance as a queryset, if the model has some unique together constraint, the is_valid call crashes
Steps to reproduce
Create a model with a unique together constraint
Create a serializer for this model
(Probably not needed) Create a custom list serializer with the update
method overwritten and provide it to your model serializer with list_serializer_class
Now use the model serializer to serialize a list of data and provide both many=True
and instance=YourModel.objects.all()
to your serializer __init__
Call serializer.is_valid(raise_exception=True)
Expected behavior
It should pass is_valid
or at least not crash
Actual behavior
It crashes in the unique together validator because instance
was not popped in the many_init so the child will receive a queryset instead of a model instance
How to fix
Popping instance
from the kwargs in many_init should do the trick but I'm not sure this won't introduce unforeseen problems
Moreover, the validators will probably fail because they will consider the call as a create rather than an update so here is that