Opened 7 years ago

Closed 6 years ago

Last modified 4 years ago

#10156 closed (fixed)

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

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

Download all attachments as: .zip

Change History (13)

Changed 7 years ago by Alex

comment:1 Changed 7 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 6 years ago by Alex

  • Status changed from new to assigned
  • Triage Stage changed from Unreviewed to Accepted

Changed 6 years ago by Alex

comment:3 Changed 6 years ago by Alex

  • milestone set to 1.1

comment:4 Changed 6 years ago by Alex

  • Triage Stage changed from Accepted to Ready for checkin

comment:5 Changed 6 years ago by jacob

  • Triage Stage changed from Ready for checkin to Accepted

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 6 years ago by russellm

  • Patch needs improvement set

comment:7 Changed 6 years ago by Alex

  • Patch needs improvement unset
  • Triage Stage changed from Accepted to Ready 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 6 years ago by jacob

  • Resolution set to fixed
  • Status changed from assigned to closed

(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 6 years ago by anonymous

  • Resolution fixed deleted
  • Status changed from closed to reopened

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 6 years ago by Alex

  • Resolution set to fixed
  • Status changed from reopened to closed

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

comment:11 Changed 4 years ago by jacob

  • milestone 1.1 deleted

Milestone 1.1 deleted

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