将 Django Count() 与 Integer 进行比较
Compare Django Count() to Integer
我正在尝试获取 True 或 False 如果特定主题有未回答的问题。
我尝试了以下方法:
以下代码对我不起作用。好像我不能比较 <type 'Count'>
和 <type 'int'>
.
Topic.objects.all().annotate(has_unanswered_questions = Count('question', filter = Q(question__status.text='open')) > 0)
您需要过滤您的注释:
Topic.objects.all()\
.annotate(unanswered_questions=Count('question', filter=Q(question__status.text='open')))\
.filter(unanswered_questions__gt=0)
我正在尝试获取 True 或 False 如果特定主题有未回答的问题。
我尝试了以下方法:
以下代码对我不起作用。好像我不能比较 <type 'Count'>
和 <type 'int'>
.
Topic.objects.all().annotate(has_unanswered_questions = Count('question', filter = Q(question__status.text='open')) > 0)
您需要过滤您的注释:
Topic.objects.all()\
.annotate(unanswered_questions=Count('question', filter=Q(question__status.text='open')))\
.filter(unanswered_questions__gt=0)