django/django — pull request #1840 by dgym (2013-11-01 14:56:53+00:00)
Title: Fixed #20849 -- ModelForms do not work well with prefetch_related.

model_to_dict() (used when rendering forms) queries the database
to get the list of primary keys for ManyToMany fields. This is
unnecessary if the field queryset has been prefetched, all the
keys are already in memory and can be obtained with a simple
iteration.

This commit can save many database queries when using a formset of ModelForms containing ManyToMany fields.