Django:RunTimeWarning:DateTimeField在时区支持处于活动状态时收到了一个天真的日期时间 [英] Django : RunTimeWarning : DateTimeField received a naive datetime while time zone support is active
问题描述
我正在尝试根据 django cart测试购物车创作
但是,当我尝试创建购物车时,我有这个错误:
RunTimeWarning:DateTimeField在时区支持处于活动状态时收到天真的日期时间
我做了一些研究,但我无法解决我的问题在我的测试目录中的 datetime.datetime.now()
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):
助手功能,所以我不重复
cart = models.Cart()
cart.creationDate = creationDate
cart.checkedOutDate = False
cart.save()
返回购物车
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:
class Cart(models.Model):
creationDate = models.DateTimeField ()
我也有 USE_TZ = True
在我的设置中。
我尝试了 timezone.now()
但仍然不起作用:
def _create_cart_in_database(self,creationDate = t imezone.now(),checkedOutDate = True):
$运行时警告:DateTimeField Cart.creationDate在时区支持处于活动状态时收到了一个天真的日期时间(2016-06-03 08:46:34.829000),而pre
def test_cart_creation(self):
creationDate = timezone.now()
编辑:
我现在有这个错误,似乎错误格式datetime?
已更新= self._save_table(raw,cls,force_insert,force_update,using,upda
te_fields )
文件C:\Python27\lib\site-packages\django-1.9.5-py2.7.egg\django\db\models\ba
se .py,第820行,_save_table
result = self._do_insert(cls._base_manager,using,fields,update_pk,raw)
文件C:\Python27\lib\site-packages \django-1.9.5-py2.7.egg\django\db\models\ba
se.py,第859行,_do_insert
using = using,raw = raw )
文件C:\Python27\lib\site-packages\django-1.9.5-py2.7.egg\django\db\models\ma
nager。 py,第122行,在manager_method
中返回getattr(self.get_queryset(),name)(* args,** kwargs)
文件C:\Python27\lib\site-packages \django-1.9.5-py2.7.egg\django\db\models\qu
ery.py,第1039行,在_insert
中返回query.get_compiler(using =使用).execute_sql(return_id)
文件C:\Python27\lib\site-packages\django-1.9.5-py2.7.egg\django\db\models\ sq
l \compiler.py,行1059,在execute_sql
中为sql,params in self.as_sql():
文件C:\Python27\lib\site- package\django-1.9.5-py2.7.egg\django\db\models\sq
l\compiler.py,第1019行,在as_sql
中为obj在自己.query.objs
文件C:\Python27\lib\site-packages\django-1.9.5-py2.7.egg\django $ db $ \\\\sq
l \ compiler.py,第958行,在prepare_value中
value = field.get_db_prep_save(value,connection = self.connection)
文件C: \Python27\lib\site-packages\django-1.9.5-py2.7.egg\django\db\models\fi
elds\__init __。py,第728行,在get_db_prep_save
prepared = False)
文件C:\Python27\lib\site-packages\django-1.9.5-py2.7.egg\django\db\\ \\ models\fi
elds\__init __。py,第1461行,get_db_prep_value
value = self.get_prep_value(value)
文件C:\Python27\lib\ site-packages\django-1.9.5-py2.7.egg\django\db\models\fi
elds\__init __。py,第1440行,get_prep_value
值= super(DateTimeField,self).get_prep_value(value)
文件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)
文件C:\Python27\lib\site-packages\django-1.9.5-py2.7.egg\ django\db\models\fi
elds\__init __。py,第1399行,to_python
parsed = parse_datetime(value)
文件C:\Python27\ lib \site-packages\django-1.9.5-py2.7.egg\django\utils\datepa
rse.py,第93行,在parse_datetime
match = datetime_re。 match(value)
TypeError:预期的字符串或缓冲区
解决方案以下行创建一个天真的(非时区知道)日期时间:
creationDate = datetime.datetime.now()
尝试将该行更改为:
creationDate = timezone.now()
不要忘记在代码开头输入
timezone
from djang o.utils import timezone
I am trying to test a cart creation based on django cart
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
I did some research but I couldn't resolve my problem for
datetime.datetime.now()
test_views.py in my tests directory :
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 :
class Cart(models.Model): creationDate = models.DateTimeField()
I also have
USE_TZ = True
in my settings.I tried
timezone.now()
but still doesn't work :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.
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
解决方案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
timezone
at the beginning of your code:from django.utils import timezone
这篇关于Django:RunTimeWarning:DateTimeField在时区支持处于活动状态时收到了一个天真的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!