Making queries using F() and timedelta at django

前端 未结 4 1619
借酒劲吻你
借酒劲吻你 2020-12-11 16:06

I have the following model:

class Process(models.Model):
  title = models.Charfield(max_length=255)
  date_up = models.DateTimeField(auto_now_add=True)
  day         


        
4条回答
  •  独厮守ぢ
    2020-12-11 17:00

    You have to extend Aggregate. Do like below:

    from django.db import models as DM
    
    class BaseSQL(object):
        function = 'DATE_SUB'
        template = '%(function)s(NOW(), interval %(expressions)s day)'
    
    class DurationAgr(BaseSQL, DM.Aggregate):
        def __init__(self, expression, **extra):
            super(DurationAgr, self).__init__(
                expression,
                output_field=DM.DateTimeField(),
                **extra
            )
    
    Process.objects.filter(date_up__lte=DurationAgr('days_activation'))
    

    Hopefully, It will work for you. :)

提交回复
热议问题