Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#15957 closed Cleanup/optimization (wontfix)

ModelChoiceField slow performance

Reported by: uriel.bertoche@… Owned by: nobody
Component: Forms Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX:


The ModelChoiceField form widget is performing in O(n)

Depending on how many entries you want displayed in your select, it takes too long. It's really annoying. I can't open my admin interface without having to wait many seconds to load all data.

[pid: 25412|app: 0|req: 10/36] () {46 vars in 911 bytes} [Tue May 3 10:33:34 2011] GET /admin/clientes/clienteobservacao/add/ => generated 3587474 bytes in 12258 msecs (HTTP/1.1 200) 6 headers in 307 bytes (0 async switches on async core 0)

[pid: 25414|app: 0|req: 12/46] () {46 vars in 834 bytes} [Tue May 3 10:34:08 2011] GET /clientes/ => generated 2806807 bytes in 9996 msecs (HTTP/1.1 200) 6 headers in 308 bytes (0 async switches on async core 0)

Change History (3)

comment:1 Changed 5 years ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to wontfix
  • Status changed from new to closed

By definition, a ModelChoiceField is going to include all elements in the queryset. I don't see how Django could do this faster than O(n). You shouldn't use ModelChoiceField for more than 1000 entries. Anyway, it becomes very quickly unusable beyond 100 entries.

See the documentation here:

comment:2 Changed 5 years ago by anonymous

changing the SQL query it makes would improve it. instead of getting all entries and then fetching their unicode one by one, you could get all entries and fetch all of them in one single SQL.

comment:3 Changed 5 years ago by aaugustin says the admin is already preloading related models:

Note that Django will use select_related(), regardless of this setting if one of the list_display fields is a ForeignKey.

If this is not true, then it's a bug in Django; could you provide a test case showing the problem?

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