1

我有一个模型,其中字段 datetime 作为参考日期,字段整数包含从参考日期开始计算的天数,我需要过滤掉 reference_date + days 小于 django orm 中当前日期的行。我尝试使用 RawSQL,将过滤器委托给 mysql,但我需要访问行的一列,而且我不知道如何在 RawSql 表达式中包含 F 表达式,我尝试加入字符串,但它不起作用. 包括我的模型描述。

class ActionData(models.Model):
    properties = models.ManyToManyField(Property, through='ActionProperties')
    action = models.ForeignKey(Action, on_delete=models.CASCADE)
    description = models.TextField(null=True)
    days = models.IntegerField(default=0)
    promocioned = models.BooleanField(default=False)
    reference_date = models.DateTimeField(null=True)
    modified_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)

    def __str__(self):
        return str(self.pk) + self.action.name
4

1 回答 1

0

这应该适合你:

ActionData.objects.filter(
    reference_date__lte=timezone.now() - (timedelta(days=1) * F('days'))
)
于 2020-05-21T18:33:40.837 回答