Django 日期时间迁移错误 [英] Django datetime migration error

查看:18
本文介绍了Django 日期时间迁移错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我的模型中没有任何变化.我已将其恢复到功能齐全的时代,但仍然出现以下错误.

I don't think anything in my model has changed. I have reverted it back to times when it was all fully functional and I still get the following errors.

有我的模型:

class UserProfile(models.Model):
    # This line is required. Links UserProfile to a User model instance.
    user = models.OneToOneField(User)

    # The additional attributes we wish to include.
    website = models.URLField(blank=True)
    picture = models.ImageField(upload_to='profile_images', blank=True)


    # Override the __unicode__() method to return out something meaningful!
    def __unicode__(self):
        return self.user.username


# Could create more post classes, or introduce foreign keys. Unsure as of now.
class Post(models.Model):
    title = models.CharField(max_length = 140)
    body = models.TextField()
    date = models.DateTimeField(blank=True)

    INTRODUCTION = 'I'
    STORIES = 'S'
    CATEGORY_CHOICES = (
        (STORIES, 'Stories'),  # Variable name and display value
        (INTRODUCTION, 'Introduce Yourself'),
    )

    category = models.CharField(max_length=1,
                                choices=CATEGORY_CHOICES,
                                default=INTRODUCTION)


    def __unicode__(self):
        return self.title


class Photo(models.Model):
    title = models.CharField(max_length = 140)
    photo = models.ImageField(upload_to='user_images', blank=True, null=True)
    date = models.DateTimeField(blank=True)

    description = models.TextField()

    def __unicode__(self):
        return self.title

在运行 manage.py migrate 时,我遇到了以下错误.检查了其他答案后,似乎与日期时间使用不正确有关,但即使我完全删除了日期和日期时间字段,错误仍然存​​在.有人有什么想法吗?

Upon running manage.py migrate I am confronted with the following error. Having checked the other answers, it seems it was something to do with datetime being used incorrectly but even if I remove date and the DateTime field altogether the error still persists. Does anyone have any ideas?

非常感谢您的帮助.

Operations to perform:
  Apply all migrations: contenttypes, admin, sessions, auth, blog
Running migrations:
  Applying blog.0007_auto_20141201_0034...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 160, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/executor.py", line 63, in migrate
    self.apply_migration(migration, fake=fake)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/executor.py", line 97, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/migration.py", line 107, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/operations/fields.py", line 37, in database_forwards
    field,
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/sqlite3/schema.py", line 160, in add_field
    self._remake_table(model, create_fields=[field])
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/sqlite3/schema.py", line 74, in _remake_table
    self.effective_default(field)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/schema.py", line 183, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 627, in get_db_prep_save
    prepared=False)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 1286, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 1269, in get_prep_value
    value = super(DateTimeField, self).get_prep_value(value)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 1171, in get_prep_value
    return self.to_python(value)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 1228, in to_python
    parsed = parse_datetime(value)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/utils/dateparse.py", line 70, in parse_datetime
    match = datetime_re.match(value)
TypeError: expected string or buffer

这里是 'blog.0007...' 迁移似乎失败了.为了澄清发生了什么,我试图添加一些属性,以告知发布者以及发布时间等.显然有些事情没有按计划进行..

Here is 'blog.0007...' migration that seems to be failing. To clarify what is going on I was attempting to add a few attributes in order to tell who posted, and at what time etc. Clearly something is not going according to plan..

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from django.conf import settings


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('blog', '0006_auto_20141201_0027'),
    ]

    operations = [
        migrations.AddField(
            model_name='post',
            name='created_by',
            field=models.ForeignKey(default=0, related_name='created_by', to=settings.AUTH_USER_MODEL),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='post',
            name='created_on',
            field=models.DateTimeField(default=0, auto_now_add=True),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='post',
            name='edited_by',
            field=models.ForeignKey(default=0, related_name='edited_by', to=settings.AUTH_USER_MODEL),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='post',
            name='edited_on',
            field=models.DateTimeField(default=0, auto_now=True),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='post',
            name='published',
            field=models.BooleanField(default=None),
            preserve_default=True,
        ),
    ]

推荐答案

我觉得问题出在这里:

DateTimeField(default=0...)

使用 Nonedatetime 对象

这篇关于Django 日期时间迁移错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆