django 固定装置日期时间字段运行时警告

标签 django runtime-error fixtures

我已经为我的 django 项目设置了一些基本装置。插入数据库的记录之一如下所示:

  {
    "model": "articles.article",
    "pk": 1,
    "fields": {
      "title": "Blackened Recordings Launches",
      "headline": "we're so psyched about our new adventure",
      "content": "<p>We like to make it a point here not to bore you with the not-so-exciting business aspects of making and sharing music, but we're so psyched about our new adventure that we just had to tell you about it as we officially launch our very own record label, Blackened Recordings.</p><p>Some of you, who have followed along throughout the years, are aware that in 1994 we renegotiated our contract with the Warner Music Group, which resulted in a joint venture with our record company for releasing all of our recordings including long form videos. Per that agreement, as of today we have taken ownership of all of our master recordings and Blackened Recordings will be the home of all of our current albums and videos along with all future releases including the December 10 release of the \"Quebec Magnetic\" DVD and Blu-ray.</p><p>You may have heard us say it once or twice or a thousand times before, but it's always been about us taking control of all things 'Tallica to give you 110% on every single level every single time. Forming Blackened Recordings is the ultimate in independence, putting us in the driver's seat of our own creative destiny. We're looking forward to making more music and getting it all out to you in our own unique way.</p>",
      "image": "examples/slide-03.jpg",
      "active": 1,
      "created_at": "2013-03-16 17:41:28"
    }
  },

这是它对应的模型:

class Article(models.Model):
    """News article, displayed on homepage to attract users"""
    class Meta:
        db_table = 'article'
    title = models.CharField(max_length=64)
    headline = models.CharField(max_length=255)
    content = models.TextField()
    image = models.ImageField(upload_to = 'articles/', null=True, blank=True)
    active = models.BooleanField()
    created_at = models.DateTimeField()
    def __unicode__(self):
        return self.title

插入 fixture 记录时,我收到以下警告:

/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py:827: RuntimeWarning: DateTimeField received a naive datetime (2013-03-16 17:41:28) while time zone support is active.
  RuntimeWarning)

我不知道这里出了什么问题。我试图关注this blog post ,但我确实安装了 pytz,并且在我的 settings.py 中有 USE_TZ=True 选项。

最佳答案

其实解决办法就深藏在python docs中,引用如下:

When serializing an aware datetime, the UTC offset is included, like this:

"2011-09-01T13:20:30+03:00"

这样的装置是完全接受的,就我而言是:

"2013-03-16T17:41:28+00:00"
"2013-03-17T23:36:12+00:00"
"2013-03-18T13:19:37+00:00"

输出是:

$ ./manage.py loaddata articles/fixtures/initial_data.json 
Installed 3 object(s) from 1 fixture(s)

请注意,'2013-03-16 17:41:28 UTC+0000' 不是正确的时区感知日期时间格式,它会给您以下错误:

DeserializationError: Problem installing fixture 'articles/fixtures/initial_data.json': [u"'2013-03-16 17:41:28 UTC+0000' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]

关于django 固定装置日期时间字段运行时警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15506858/

相关文章:

jsf - 客户端 ID : j_id10 is duplicated in the faces tree. 组件

php - MYSQL执行fixtures load后超时

python - 如何定义一个给定测试子目录中的所有测试使用的 pytest 固定装置?

python - 有没有一种 Django 方法可以将测试结果与 fixture 进行比较?

javascript - Django:如何从 Django View 中的 ajax 调用中检索序列化的复选框值?

python - Django + InnoDB : random failure of model reading

scala - java.lang.String is not a valid external type for schema of int error in creating spark 数据帧

python - 乘以 Django Apache 服务器

django - 用于 SMTP 的 G Suite 和 Django

arrays - 为什么 Rust 在运行时检查数组边界,而(大多数)其他检查发生在编译时?