#15957 closed Cleanup/optimization (wontfix)
ModelChoiceField slow performance
Reported by: | 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: | no |
Description
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] 192.168.1.251 () {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] 192.168.1.251 () {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 by , 14 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
comment:2 by , 14 years ago
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 by , 14 years ago
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_select_related 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?
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: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.raw_id_fields