Python Django:RunTimeWarning:DateTimeField 在时区支持处于活动状态时收到了一个天真的日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37607411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:39:09  来源:igfitidea点击:

Django : RunTimeWarning : DateTimeField received a naive datetime while time zone support is active

pythondjango

提问by William P.

I am trying to test a cart creation based on django cart

我正在尝试测试基于django 购物车的购物车创建

But I have this error when I am trying to create the cart :

但是当我尝试创建购物车时出现此错误:

RunTimeWarning : DateTimeField received a naive datetime while time zone support is active

RunTimeWarning : DateTimeField 在时区支持处于活动状态时收到了一个天真的日期时间

I did some research but I couldn't resolve my problem for datetime.datetime.now()

我做了一些研究,但我无法解决我的问题 datetime.datetime.now()

test_views.py in my tests directory :

test_views.py 在我的测试目录中:

from django.test import TestCase, Client, RequestFactory
import unittest
from django.contrib.auth.models import User, AnonymousUser
from front.models import Entreprise, Cart, CartItems
from decimal import Decimal
from front.cart import models
import datetime
import pytz
from pytz import all_timezones
from django.utils import timezone



def _create_cart_in_database(self, creationDate=datetime.datetime.now(), checkedOutDate=True):
    """
        Helper function so I don't repeat myself
    """
    cart = models.Cart()
    cart.creationDate = creationDate
    cart.checkedOutDate = False
    cart.save()
    return cart


def test_cart_creation(self):
    creationDate = datetime.datetime.now()
    cart = self._create_cart_in_database(creationDate)
    id = cart.id

    cart_from_database = models.Cart.objects.get(pk=id)
    self.assertEquals(cart, cart_from_database)

models.py :

模型.py:

class Cart(models.Model):
    creationDate = models.DateTimeField()

I also have USE_TZ = Truein my settings.

USE_TZ = True我的设置里也有。

I tried timezone.now()but still doesn't work :

我试过了,timezone.now()但仍然不起作用:

def _create_cart_in_database(self, creationDate=timezone.now(), checkedOutDate=True):

def test_cart_creation(self):
    creationDate = timezone.now()

RunTimeWarning : DateTimeField Cart.creationDate received a naive datetime (2016-06-03 08:46:34.829000) while time zone support is active.

RunTimeWarning : DateTimeField Cart.creationDate 在时区支持处于活动状态时收到了一个天真的日期时间 (2016-06-03 08:46:34.829000)。

EDIT :

编辑 :

I have this error now and it seems an error format datetime ?

我现在有这个错误,它似乎是日期时间格式错误?

    updated = self._save_table(raw, cls, force_insert, force_update, using, upda
te_fields)
  File "C:\Python27\lib\site-packages\django-1.9.5-py2.7.egg\django\db\models\ba
se.py", line 820, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "C:\Python27\lib\site-packages\django-1.9.5-py2.7.egg\django\db\models\ba
se.py", line 859, in _do_insert
    using=using, raw=raw)
  File "C:\Python27\lib\site-packages\django-1.9.5-py2.7.egg\django\db\models\ma
nager.py", line 122, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Python27\lib\site-packages\django-1.9.5-py2.7.egg\django\db\models\qu
ery.py", line 1039, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "C:\Python27\lib\site-packages\django-1.9.5-py2.7.egg\django\db\models\sq
l\compiler.py", line 1059, in execute_sql
    for sql, params in self.as_sql():
  File "C:\Python27\lib\site-packages\django-1.9.5-py2.7.egg\django\db\models\sq
l\compiler.py", line 1019, in as_sql
    for obj in self.query.objs
  File "C:\Python27\lib\site-packages\django-1.9.5-py2.7.egg\django\db\models\sq
l\compiler.py", line 958, in prepare_value
    value = field.get_db_prep_save(value, connection=self.connection)
  File "C:\Python27\lib\site-packages\django-1.9.5-py2.7.egg\django\db\models\fi
elds\__init__.py", line 728, in get_db_prep_save
    prepared=False)
  File "C:\Python27\lib\site-packages\django-1.9.5-py2.7.egg\django\db\models\fi
elds\__init__.py", line 1461, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\Python27\lib\site-packages\django-1.9.5-py2.7.egg\django\db\models\fi
elds\__init__.py", line 1440, in get_prep_value
    value = super(DateTimeField, self).get_prep_value(value)
  File "C:\Python27\lib\site-packages\django-1.9.5-py2.7.egg\django\db\models\fi
elds\__init__.py", line 1296, in get_prep_value
    return self.to_python(value)
  File "C:\Python27\lib\site-packages\django-1.9.5-py2.7.egg\django\db\models\fi
elds\__init__.py", line 1399, in to_python
    parsed = parse_datetime(value)
  File "C:\Python27\lib\site-packages\django-1.9.5-py2.7.egg\django\utils\datepa
rse.py", line 93, in parse_datetime
    match = datetime_re.match(value)
TypeError: expected string or buffer

回答by Selcuk

The following line creates a naive (non-timezone aware) datetime:

以下行创建了一个天真的(非时区感知)日期时间:

creationDate = datetime.datetime.now()

Try changing that line to:

尝试将该行更改为:

creationDate = timezone.now()

Don't forget to import timezoneat the beginning of your code:

不要忘记timezone在代码的开头导入:

from django.utils import timezone