简体   繁体   English

从父查询集中检索 prefetch_related 对象的平面查询集

[英]Retrieve a flat Queryset of prefetch_related objects from a parent queryset

If I have models like so:如果我有这样的模型:

class A(models.Model):
  ...


class B(models.Model):
  a = models.ForeignKey(A)


class C(models.Model): 
  b = models..ForeignKey(B)

I can get the full queryset:我可以获得完整的查询集:

qs = A.objects.all().prefetch_related('b_set', 'b_set__c_set')
>> <QuerySet [<A: A object (1)>, <A: A object (2)>, ... ]>

What I want to know is there a way to get all the C objects in a flattened queryset, eg:我想知道有没有一种方法可以在扁平查询集中获取所有C对象,例如:

qs['b_set__c_set']
>> <QuerySet [<C: C object (1)>, <C: C object (2)>, ... ]>

You could do a query on the C Model, just checking for all C's that have an a value?您可以对 C 模型进行查询,只检查所有具有a值的 C? So所以

C.objects.filter(b__is_null=False, b__a__isnull=False)

Would that achieve what you're looking for?这会实现你想要的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM