25 ms·
Also worth looking at prefetch_related(), which landed in 1.4, which can be used to alleviate common O(n) situations: https://docs.djangoproject.com/en/dev/ref
by insin 14y ago
Also worth looking at prefetch_related(), which landed in 1.4, which can be used to alleviate common O(n) situations:
https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.prefetch_related https://docs.djangoproject.com/en/dev/ref/models/querysets/#...
I've also used this in the past to do something similar, grabbing the ids of all specified related fields (related to the same model type) and pulling them back in a single query, with the option to only grab certain columns as dicts instead of full model instances if you're only selecting additional data required for specific templates:
http://djangosnippets.org/snippets/1117/ http://djangosnippets.org/snippets/1117/