-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
#4917 - Remove O(n) queries in m2m updates #5093
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -492,10 +492,19 @@ def to_internal_value(self, data): | |
if not self.allow_empty and len(data) == 0: | ||
self.fail('empty') | ||
|
||
return [ | ||
self.child_relation.to_internal_value(item) | ||
for item in data | ||
] | ||
if isinstance(self.child_relation, PrimaryKeyRelatedField): | ||
values = list(self.child_relation.get_queryset().filter(pk__in=data)) | ||
missing_primary_keys = set(v.pk for v in values) - set(data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you use set for unique list, why not use distinct on values filter? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are not bothered about distinct values, |
||
|
||
if missing_primary_keys: | ||
self.fail('missing_ids', ids_not_found=list(missing_primary_keys)) | ||
else: | ||
values = [ | ||
self.child_relation.to_internal_value(item) | ||
for item in data | ||
] | ||
|
||
return values | ||
|
||
def get_attribute(self, instance): | ||
# Can't have any relationships if not created | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use
values_list('pk', flat=True)
here to just fetch the ids and skip object instantiation?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because we then return the values below (line 507), which need to be full model instances.