I have this project where, I need to fetch multiple objects from multiple models in a view. I could do that by for loop but I think I shouldn't hit database in each loop. Should I use prefetch_related. or Should I know some other way to retrieve them.
for example:
class A(models.Model):
title_name=models.CharField(...)
id=models.AutoField(pk=True)
class B(models.Model):
user=models.ForeignKey(User,models.ON_CASCADE=True)
user_status=models.CharField(...)
id=models.ForeignKey(A, models.ON_CASCADE=True)
I need to display user_status, user and associated title_name. I get multiple objects, select_related will not be useful. Any suggestions.
prefetch_related. There is no better way than thatprefetch_related, i couldn't exactly do what i wanted to, I've made a query something like B.objects.get(user=request.user).prefetch_related('id')