Feeding prefetched results into new queryset

10 views
Skip to first unread message

Дилян Палаузов

unread,
Jul 23, 2016, 8:44:39 AM7/23/16
to Django users
Hello,

for the rendering of one View I want to execute two queries:

class A(model.Model):
i = models.CharField(max_length=10)

class B(model.Model):
f = models.ManyToManyField(A, on_delete...)
j = models.CharField(max_length=10)


- All objects of type A
queryset1 = A.objects.all()
- All objects of type B prefetching the related objects of type A:
queryset2 = B.objects.prefetch_related('f')

Under these conditions the table A is SELECTEd twice: once all elements are picked up and the result is cached in queryset1. The second time the columns of table A are touched again, if they stay in relation to B objects.

Is there any way to feed into queryset2 the (prefetched) results from queryset1, so that the second SELECT ... FROM A can be prevented (and the join can still happen python-side in RAM)?

Thanks in advance for your answer
Dilian
Reply all
Reply to author
Forward
0 new messages