1👍
Use prefetch_related()
; select
doesn’t work on
_related()ManyToManyField
s:
providers = Provider.objects.order_by('provider').all().prefetch_related('userprofile_set')
From the docs:
Returns a
QuerySet
that will automatically retrieve, in a single batch, related objects for each of the specified lookups.This has a similar purpose to
select_related
, in that both are designed to stop the deluge of database queries that is caused by accessing related objects, but the strategy is quite different.…
prefetch_related
, on the other hand, does a separate lookup for each relationship, and does the ‘joining’ in Python. This allows it to prefetch many-to-many and many-to-one objects, which cannot be done usingselect_related
, in addition to the foreign key and one-to-one relationships that are supported byselect_related
.
0👍
I’m not using django but i just found the answer here: docs.djangoproject.com
providers = Provider.objects.select_related().order_by('provider').all()
- [Answer]-Why does Django only create one "REFERENCES" clause on a ManyToMany table?
- [Answer]-Django-Simple-Friends all users not connected as friends
- [Answer]-Getting model default value from modelForm in django
- [Answer]-Extra form fields added in Django form render
- [Answer]-Comparing datetime field in python, not just date or time , Type Error :Can't compare unicode to date.datetime field