简体   繁体   English

pycharm IDE warning on django doc: Expected type 'timedelta', got 'DateTimeField' instead python 3.7 pycharm 2019.3

[英]pycharm IDE warning on django doc: Expected type 'timedelta', got 'DateTimeField' instead python 3.7 pycharm 2019.3

in Django's doc is the following code snippetDjango 的文档中是以下代码片段

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


class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __str__(self):
        return self.question_text

    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

Pycharm highlights self.pub_date and shouts Pycharm 高亮self.pub_date并大喊

Expected type 'timedelta', got 'DateTimeField' instead预期类型“timedelta”,改为“DateTimeField”

How do I get rid of this and do things right?我该如何摆脱这种情况并正确地做事?

While the code is functional, it suffers from a typing issue which PyCharm is highlighting.虽然代码可以正常工作,但它遇到了 PyCharm 突出显示的打字问题。 A ticket was raised to Django to correct the documentation but was closed without implementing.已向 Django 提出票证以更正文档,但未实施就关闭了。

The solution is also in the Django ticket, copied here for completeness.解决方案也在 Django 票证中,为了完整起见,复制到这里。

  • return self.pub_date >= timezone.now() - datetime.timedelta(days=1)返回 self.pub_date >= timezone.now() - datetime.timedelta(days=1)

becomes变成

  • return self.pub_date >= (timezone.now() - datetime.timedelta(days=1)).timestamp() return self.pub_date >= (timezone.now() - datetime.timedelta(days=1)).timestamp()

When using the Steven B.'s solution, it throws a TypeError exception ('>=' not supported between instances of 'datetime.datetime' and 'float').使用 Steven B. 的解决方案时,它会引发 TypeError 异常(在 'datetime.datetime' 和 'float' 的实例之间不支持'>=')。

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


class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __str__(self):
        return self.question_text

    def was_published_recently(self):
        return self.pub_date >= (datetime.datetime.now() - datetime.timedelta(days=1)).timestamp()

So when comparing datetimes in Django, we have to keep in mind that the datetime object is naive in Python by default, so you need to make both of them either naive or aware datetime objects.因此,在比较 Django 中的日期时间时,我们必须记住,日期时间 object 在 Python 中是幼稚的,因此您需要了解它们两者。 All credit to Viren Rajput in this answer 这个答案中的所有功劳都归功于 Viren Rajput

Because of that, the following solution works correctly:因此,以下解决方案可以正常工作:

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


class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __str__(self):
        return self.question_text

    def was_published_recently(self):
        return self.pub_date >= (timezone.now() - datetime.timedelta(days=1)).replace(tzinfo=pytz.UTC)

Note: If we cast both expressions to float using the timestamp() method:注意:如果我们使用 timestamp() 方法将两个表达式都转换为浮点数:

from django.db import models
import datetime


class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __str__(self):
        return self.question_text

    def was_published_recently(self):
        return self.pub_date.timestamp() >= (datetime.datetime.now() - datetime.timedelta(days=1)).timestamp()

(It could work, but PyCharm would point out the warning Unresolved attribute reference 'timestamp' for class 'DateTimeField') (它可以工作,但 PyCharm 会指出警告未解析的属性引用 class 'DateTimeField')

Django version: 3.2.5 Django 版本:3.2.5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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