Opened 8 years ago

Closed 7 years ago

Last modified 5 years ago

#10156 closed (fixed)

ModelMultipleChoiceField does O(n) queries where O(1) will suffice

Reported by: Alex Gaynor Owned by: Alex Gaynor
Component: Forms Version: 1.0
Severity: Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Which is hugely inefficient, selecting 20 items already means you're going outside the reasonable limits for SQL queries.

Attachments (2)

multiple-model-choice-field.diff (852 bytes) - added by Alex Gaynor 8 years ago.
multiple-model-choice-field.2.diff (1.2 KB) - added by Alex Gaynor 8 years ago.

Download all attachments as: .zip

Change History (13)

Changed 8 years ago by Alex Gaynor

comment:1 Changed 8 years ago by Joes

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

Had to develop similar solution (and custom widget) to get rid of mentioned issue.

comment:2 Changed 8 years ago by Alex Gaynor

Status: newassigned
Triage Stage: UnreviewedAccepted

Changed 8 years ago by Alex Gaynor

comment:3 Changed 8 years ago by Alex Gaynor

milestone: 1.1

comment:4 Changed 8 years ago by Alex Gaynor

Triage Stage: AcceptedReady for checkin

comment:5 Changed 8 years ago by Jacob

Triage Stage: Ready for checkinAccepted

This isn't quite correct yet. In the patched version, self.queryset.filter(pk=pk) will never raise the ValueError the try/except block is catching against, so the logic is incorrect somewhere.

comment:6 Changed 7 years ago by Russell Keith-Magee

Patch needs improvement: set

comment:7 Changed 7 years ago by Alex Gaynor

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

Jacob's complaint was not accurate as the tests found here:

http://code.djangoproject.com/browser/django/trunk/tests/modeltests/model_forms/models.py#L827

Still pass, and they obviously rely on that ValueError logic being correct(the reason this works is get_db_prep_lookup is called on the object being added to the QuerySet, not on the Query being executed).

comment:8 Changed 7 years ago by Jacob

Resolution: fixed
Status: assignedclosed

(In [10582]) Fixed #10156: ModelMultipleChoiceField.clean now does a single query instead of O(N). Thanks, Alex Gaynor. Also, I ported a few more doctests to unittests.

comment:9 Changed 7 years ago by anonymous

Resolution: fixed
Status: closedreopened

r10482 uses filter which doesn't return same order as pks given, where as previous implementation appended values to the end. e.g.: clean([3,2]) -> {object 3, object 2} where as new implementation returns {object 2, object 3}
Its a changed in the behavior, which I was using ...

comment:10 Changed 7 years ago by Alex Gaynor

Resolution: fixed
Status: reopenedclosed

This ticket was fixed, if you believe there is a new issue please file a new ticket.

comment:11 Changed 5 years ago by Jacob

milestone: 1.1

Milestone 1.1 deleted

Note: See TracTickets for help on using tickets.
Back to Top