Django与F对象的时差 [英] Django time difference with F object

查看:125
本文介绍了Django与F对象的时差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下模型:

 类赋值(models.Model):
extra_days = models.IntegerField (default = 0)
due_date = models.DateTimeField()

其中 due_date 是作业到期的日期, extra_days 是完成作业到期日后给定的额外天数。 p>

我想创建一个返回所有行的查询,其中 due_date + extra_days 大于当前日期。这是我在做什么:

  from django.utils import timezone 
from django.db.models import F
from datetime import datetime

cur_date = timezone.make_aware(datetime.now(),timezone.get_default_timezone())
a = Assignment.objects.filter(extra_days__gt = cur_date - F(' )

当我打印 a ,我收到以下错误:

 文件c:\Python27\lib\site- packages\MySQLdb\\ \\ cursors.py,行204,执行
如果不是self._defer_warnings:self._warning_check()
文件c:\Python27\lib\site-packages\MySQLdb\ cursors.py,第117行,_warning
_check
warn(w [-1],self.Warning,3)
警告:截断不正确的DOUBLE值:'2013-09-01 02:54:31'

如果我做出时差,比如说3.1天,我假设天的差异将仍然是3.我thi nk会更正确的做这样的事情:

  a = Assignment.objects.filter(due_date__gt = cur_date  -  timedelta = F('extra_days'))

但这也会导致错误。



如何在不编写原始SQL查询的情况下执行此操作?

解决方案

像我想要做的是不可能的。我最终写了一个原始查询:

  cursor.execute(SELECT * FROM app_assignment WHERE DATE_ADD(due_date,INTERVAL extra_days DAYS) > utc_timestamp())

我无法使用ORM进行排除这样看起来很简单,我考虑尝试SQLAlchemy,但是一个原始查询工作正常。我经常尝试解决方法,以确保我可以使用ORM,但是我将使用原始的SQL向前进行复杂的查询。


I have the following model:

class Assignment(models.Model):
  extra_days = models.IntegerField(default=0)
  due_date = models.DateTimeField()

Where due_date is the date the assignment is due and extra_days is the number of extra days given after the due date to finish the assignment.

I want to create a query that returns all rows where due_date + extra_days is greater than the current date. Here's what I am doing:

from django.utils import timezone
from django.db.models import F
from datetime import datetime

cur_date = timezone.make_aware(datetime.now(), timezone.get_default_timezone())
a = Assignment.objects.filter(extra_days__gt=cur_date - F('due_date'))

When I print a, I get the following error:

  File "c:\Python27\lib\site-packages\MySQLdb\cursors.py", line 204, in execute
    if not self._defer_warnings: self._warning_check()
  File "c:\Python27\lib\site-packages\MySQLdb\cursors.py", line 117, in _warning
_check
    warn(w[-1], self.Warning, 3)
Warning: Truncated incorrect DOUBLE value: '2013-09-01 02:54:31'

If I do a time difference that results in, say, 3.1 days, I'm assuming the days difference would be still be 3. I think it would more correct to do something like this:

a = Assignment.objects.filter(due_date__gt=cur_date - timedelta(days=F('extra_days')))

But that also results in an error.

How can I do this without writing a raw SQL query?

解决方案

It seems like what I'm trying to do is not possible. I ended up writing a raw query:

cursor.execute("SELECT * FROM app_assignment WHERE DATE_ADD(due_date, INTERVAL extra_days DAYS) > utc_timestamp()")

I was so repulsed at not being able to use the ORM for doing something so seemingly simple that I considered trying out SQLAlchemy, but a raw query works fine. I always tried workarounds to make sure I could use the ORM, but I'll use raw SQL going forwards for complex queries.

这篇关于Django与F对象的时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆