[英]Django: filter ForeignKey in value or None
我能否在不訴諸兩個查詢的情況下使以下內容起作用?
>>> 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
使用Q
對象 。
from django.db.models import Q
len(Document.objects.filter( Q(category=c) | Q(category=None) ) )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.