[英]How to perform 'SELECT a or b < c FROM Table' on Django QuerySet?
I have the following model:我有以下 model:
class DiscountCoupon(models.Model):
is_used = models.BooleanField(default=False)
expiration_date = models.DateField()
def is_available(self):
if self.used or self.expiration_date <= date.today():
return False
return True
Instead of calling the is_available
method iterating through a QuerySet, I want to do this operation into the QuerySet to perform better, like this:我不想在 QuerySet 中调用
is_available
方法迭代,而是希望在 QuerySet 中执行此操作以更好地执行,如下所示:
SELECT (used OR (expiration_date < Now())) AS is_available FROM randomapp_discountcoupon;
Is this possible?这可能吗?
You can use Q
objects as您可以将
Q
对象用作
from django.db.models import Q
discount_objects = DiscountCoupon.objects.filter(Q(is_used=True) | Q(expiration_date__lt=datetime.date.today())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.