django - prefetch_related with nested m2m -


given following models improve performance of filter avoid unnecessary calls db.

class a(models.model):     name = models.charfield()     b = manytomanyfield(b)  class b(models.model):     c = manytomanyfield(c)  class c(models.model):     d = manytomanyfield(d)  class d(models.model):     foo = models.textfield() 

how achieve prefetch_related?

''.join(a.objects.filter(b__c__d=self.d).prefetch_related('??').values_list('name', flat=true))

it works in similar fashion filter (b__c__d). prefetch:

''.join(a.objects.filter(b__c__d=self.d).prefetch_related('b__c__d').values_list('name', flat=true)) 

Comments