Opened 15 years ago

Closed 15 years ago

Last modified 13 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: no UI/UX: no

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 15 years ago.
multiple-model-choice-field.2.diff (1.2 KB ) - added by Alex Gaynor 15 years ago.

Download all attachments as: .zip

Change History (13)

by Alex Gaynor, 15 years ago

comment:1 by Joes, 15 years ago

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

comment:2 by Alex Gaynor, 15 years ago

Status: newassigned
Triage Stage: UnreviewedAccepted

by Alex Gaynor, 15 years ago

comment:3 by Alex Gaynor, 15 years ago

milestone: 1.1

comment:4 by Alex Gaynor, 15 years ago

Triage Stage: AcceptedReady for checkin

comment:5 by Jacob, 15 years ago

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 by Russell Keith-Magee, 15 years ago

Patch needs improvement: set

comment:7 by Alex Gaynor, 15 years ago

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 by Jacob, 15 years ago

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 by anonymous, 15 years ago

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 by Alex Gaynor, 15 years ago

Resolution: fixed
Status: reopenedclosed

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

comment:11 by Jacob, 13 years ago

milestone: 1.1

Milestone 1.1 deleted

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