【发布时间】:2022-10-05 12:24:16
【问题描述】:
我有一个用户模型,其中包含这样的字段,
is_active = models.BooleanField()
date_joined = models.DateTimeField(auto_now_add=True)
resigned_date = models.DateTimeField(blank=True, null=True)
如果is_active 字段为True,则resigned_date 将为None。如果is_active 字段为False,则resigned_date 字段将包含日期。我想我可以解释一下。
我想要的是基于一些 DATE 和 is_active 字段的用户查询集。
更清楚地,我想获取(active 和 joined_date 小于或等于当前日期)和(不是 active 和 resigned_date 大于当前日期)的员工列表
到目前为止我写的查询:
users = user_models.User.objects.filter(
Q(
is_active=True,
date_joined__month__lte=month,
date_joined__year__lte=year,
)
& Q(
is_active=False,
resigned_date__month__gt=month,
resigned_date__year__gt=year,
)
)
但这不起作用。它不会返回任何用户。
我怎样才能做到这一点?
谢谢
-
这段代码的输出是什么?
-
没有什么。只是一个黑色查询列表
标签: python django django-models django-queryset django-orm