For reducing the number of queries when people access frequently queried foreign key references, I've created a custom manager which includes select_related
always for the foreign key relation.
Now that there are places in code where people have used only
on this same model when creating a query set. How can I override only
query set method to include this foreign key relation always.
class CustomManager(models.Manager):
def get_queryset(self):
return super(CustomManager, self).get_queryset().select_related('user')
When this model is used as below, I run into an error which says Field cannot be both deferred and traversed using select_related at the same time.
Model.objects.only('id','field_1').get(pk=1)
Anyway to mitigate this? I've to use select_related
as above, since it will save lot of queries.