我试图测试一个基于django cart的购物车创建

但我在try 创建购物车时出现了以下错误:

RunTimeWarning:DateTimeField在时区支持处于活动状态时收到一个原始的datetime

我做了一些研究,但我datetime.datetime.now()年都没能解决我的问题

测试视图.在我的测试目录中:

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)

模型.py:

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

我的设置中也有USE_TZ = True个.

我试了timezone.now()次,但还是不行:

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

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

RunTimeWarning:DateTimeField购物车.creationDate在时区支持处于活动状态时收到一个原始日期时间(2016-06-03 08:46:34.829000).

编辑:

我现在有这个错误,它似乎是一个错误格式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

推荐答案

下一行创建了一个朴素的(不知道时区的)日期时间:

creationDate = datetime.datetime.now()

try 将该行更改为:

creationDate = timezone.now()

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

from django.utils import timezone

Python-3.x相关问答推荐

如何从Django连接到Neo4J s AuraDB(免费层)?'

使用Pandas 阅读Excel定义的名称和单元格值(&Q;)

Python将类实例变量转换为嵌套 struct

如何将python点击参数设置为与选项回调不同的参数的别名?

我用Kivy创建的应用程序在安卓系统上运行时出错.(attributeerror:';class';对象没有属性';_javaclass__cls_storage';)

在 Python 中比较和排序列之间的值(带有不匹配列)

调用 Clear 时 Airflow 会加载新代码吗

两个 y 轴在零处对齐的 plotly barplot

在不改变 python 中原始数组顺序的情况下,对多维字符串数组进行降序排序?

Keras 中 Conv2D 层的意外结果

获取以特定字母开头的姓氏

python用户输入5个偶数并打印最大的

sys.stdin.readline() 和 input():读取输入行时哪个更快,为什么?

导入 python 模块而不实际执行它

Python在OrderedDict中 Select 第i个元素

plt.cm.get_cmap 中可以使用哪些名称?

从 csv 中删除单行而不复制文件

python - Pandas - Dataframe.set_index - 如何保留旧的索引列

Python 3.4 多处理队列比 Pipe 快,出乎意料

Python,Docker - ascii编解码器无法编码字符