#18949 closed Cleanup/optimization (fixed)
model_to_dict is slow for tables with very large fields
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Forms | Version: | 1.4 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
In ModelForms, when populating initial data using django.forms.models.model_to_dict, tables with very large text fields can slow things down quite a bit. This can be fixed by using the values_list method of a queryset. The current line:
data[f.name] = [obj.pk for obj in f.value_from_object(instance)]
could be written:
data[f.name] = f.value_from_object(instance).values_list('pk', flat=True)
Change History (8)
comment:1 by , 12 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 12 years ago
comment:4 by , 12 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Note:
See TracTickets
for help on using tickets.
Note that while recently using a similar construct in some code, I discovered that values_list does not actually return a list. Normally this would be a good thing, but in some cases this could cause backward incompatible behavior. It might be preferable to cast the return value to a list before returning it to maintain 100% backward compatibility.