如何为注释django提供返回布尔字段的条件



我正在尝试为注释字段提供返回BooleanField的条件

class Employee(models.Model):
date_of_expire = models.DateTimeField()

我的观点.py

from django.db.models import Case,When,BooleanField
def lists(request):
lists = Employee.objects.annotate(is_expire=Case(When(
date_of_expire__lte=timezone.now()
),output_field=BooleanField())).order_by('-date_of_expire')
#others

但它不起作用,仍然返回所有现有的数据,即使对象的某些date_of_expire小于当前时间

还有什么我应该试试的吗?

@willem Van Onsem先生在评论中提到了一个链接

from django.db.models import BooleanField,ExpressionWrapper,Q
lists = Employee.objects.annotate(is_expire=ExpressionWrapper(Q(date_of_expire__lte=timezone.now()),output_field=BooleanField()).order_by('-date_of_expire')

ExpressionWrapper

最新更新