我可以在不诉诸两个查询的情况下获得以下工作吗?Django:过滤外键值或无
>>> c = Category.objects.all()[0]
>>> len(Document.objects.filter(category=c))
3
>>> len(Document.objects.filter(category=None))
55
>>> len(Document.objects.filter(category__in=[c, None]))
3
我可以在不诉诸两个查询的情况下获得以下工作吗?Django:过滤外键值或无
>>> c = Category.objects.all()[0]
>>> len(Document.objects.filter(category=c))
3
>>> len(Document.objects.filter(category=None))
55
>>> len(Document.objects.filter(category__in=[c, None]))
3
预期结果是什么? – aisbaa 2012-04-17 18:33:33
如果你想要所有类别的计数,'Document.objects.values('category')。annotate(count = Count('category'))。order_by()' – agf 2012-04-17 19:08:08
实际上并不需要计数。这仅仅是为了说明的目的。 – 2012-04-17 19:22:59