You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Optimize out O(n) queries when updating ManyRelatedField #4917
I think it was designed this way to throw a reasonable error if a related PK does not exist, and to be as extensible as possible.
However I think the most common case of using a ManyRelatedField isn't served well by this and could be improved. The most common case would be just a ManyRelatedField with the related objects pk as the value, and the current implementation will make N queries for the models.
And to override this you would need to create a custom field and override the to_internal_value function to fetch it.
For this common case wouldn't something like this implementation be suitable, and couldn't something like this be included in DRF?
Just use pk__in to fetch all the models in a single query, then compare the length of the result with the length of the given data. If they don't match then we are missing a record and we can use a set operator to find out which ones and raise a validation error with the missing primary keys?
Yeah, good point, I guess you should just use the set difference from the get-go. It's more about using '__in' instead of repeated single queries though.
It's probably worth throwing up a pull request with this suggestion. If it doesn't change any API and it doesn't break any tests then it sounds like we'd be good. 👍
I can give it a go and see if anything fails, but I'm not sure how to handle the self.child_relation.to_internal_value call in the original code. That is missing and that would likely break something, somewhere.
Well, I got all the tests to pass by using an isinstance check for PrimaryKeyRelatedField. Not sure if this is the best way, using the pk_optimization stuff didn't seem to work.
Activity
lovelydinosaur commentedon Apr 24, 2017
Couldn't they be the same length but different values?
orf commentedon Apr 24, 2017
Yeah, good point, I guess you should just use the set difference from the get-go. It's more about using '__in' instead of repeated single queries though.
E.g:
Or something to that effect. Perhaps I'm missing the point and there is a good reason for the repeated queries though?
lovelydinosaur commentedon Apr 24, 2017
It's probably worth throwing up a pull request with this suggestion. If it doesn't change any API and it doesn't break any tests then it sounds like we'd be good. 👍
orf commentedon Apr 24, 2017
I can give it a go and see if anything fails, but I'm not sure how to handle the
self.child_relation.to_internal_valuecall in the original code. That is missing and that would likely break something, somewhere.orf commentedon Apr 24, 2017
Well, I got all the tests to pass by using an
isinstancecheck forPrimaryKeyRelatedField. Not sure if this is the best way, using thepk_optimizationstuff didn't seem to work.Proof of Concept for single lookup on HyperlinkRelatedField
Proof of Concept for single lookup on HyperlinkRelatedField
Proof of Concept for single lookup on HyperlinkRelatedField
carltongibson commentedon Jul 10, 2017
Closing as a known limitation in line with #5150. (Full featured PRs welcomed.)