简体   繁体   中英

Django query : “datetime + delta” as an expression

Well, my question is a bit as follows:

Suppose I have the next model, this is a simple case:

class Period(models.Model):
    name = CharField(... field specs here ...)
    start_date = DateTimeField(... field specs here ...)
    duration = ... don't know what field should i use but this has to hold a delta ...

I would like to retrieve objects where (start_date + duration) < now in a django query (i know i can, in python, test like my_date_time + a_time_delta < datetime.now(), but this is not the case ... i need it as part of the query).

How can i get it?

我认为您的快速答案是从您现在的datetime中减去而从模型datetime中添加,如下所示:

.filter(start_date__lte=datetime.now()-timedelta(days=duration))

As your filter depends of two attributes of the same model, you would need to use an F() expression for the duration part and at the same time combine it with some sort of timedelta . That's not supported in the Django ORM at the moment.

You can use an extra() call, though. The disadvantage of this approach is that it is not database-agnostic. This is for PostgreSQL:

Period.objects.extra(
    where=["start_date + duration * INTERVAL '1 day' < %s"],
    params=[datetime.now()]
)

assuming that your duration is in days. If not, adjust accordingly.

You may want to avoid using extra in your Django ORM queries, so you can apply next workaround:

1) add calculated field and update it on each model's save (you should adjust save logic according to your logic)

class Period(models.Model):
    name = CharField()
    start_date = DateTimeField()
    duration = IntegerField(help_text='hours')
    end_date = DateTimeField()

def save(self, *args, **kwargs):
    self.end_date = self.start_date + datetime.timedelta(days=self.duration)
    super(Period, self).save(*args, **kwargs)

2) then use calculated field in your queries:

finished = Period.objects.filter(end_date__lt=datetime.datetime.now())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM