-
Notifications
You must be signed in to change notification settings - Fork 116
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
Validation of child runs twice. Is this deliberate as it seems a bit inefficient to run the same code twice. #164
Comments
Something else that I am observing is that, when performing update operations, the first run of |
Oh yeah. I was also actually trying to use self.instance when I realised that it runs twice. I'm, for now, ignoring the validation when there is no self.instance; I don't know what the consequences of that are going to be. |
I have conditional validation that changes depending upon whether the child was being updated or created and I am reliant on Example: def validate(self, data):
pk = data.get('id') or data.get('pk')
self_exists = bool(self.instance) or self.Meta.model.objects.filter(pk=pk).exists() The only alternative that I can see is what you're doing @tomgwasira: ignore/skip validation if |
The same situation applies when I try to use It could be very useful if you split code in |
Would love to see a fix for this. I see a PR exists to address it currently #174 but not sure if it is the ideal solution. |
Took the time to really dig into how it works and realized that the double validation occurs because when dealing with List Serializers; the child serializer that is attached to the list serializer, which is basically the same serializer minus the many=True flag, is instantiated each time for each object in the list and then validation is also called each time. So the sequence of events is the ListSerializer will validate all the objects in the list and then the process is repeated for each object with a new instance of the serializer again. This is done because by default you cannot reuse a serializer once you call save among other blockers that are placed by DRF. It's not the most efficient approach and will cause issues when trying to pass custom params to the serializer constructor and then access them later on in the process. Its a difficult task to solve but more work is needed on this library for sure. I really appreciate the work done on it so far and it will work for simple use cases but anything too complex and you will start having more issues. |
Firstly, huge thanks for the code.
I have a small question. Given two serializers: a parent and a child, inserting a
validate
method in the child serializer and printing something from there reveals that the function is called twice. Is this deliberate as it seems a bit inefficient to run the same code twice.The text was updated successfully, but these errors were encountered: