Django日期时间迁移错误

时间:2014-12-01 01:20:00

标签: django datetime migration migrate

我认为模型中的任何内容都没有改变。我已将其恢复到完全正常运行的时间,但仍然会出现以下错误。

有我的模特:

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后,我遇到以下错误。检查了其他答案后,似乎与日期时间错误使用有关,但即使我删除了日期和DateTime字段,错误仍然存​​在。有没有人有任何想法?

非常感谢你的帮助。

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 ...'迁移似乎失败了。为了澄清发生了什么,我试图添加一些属性,以便告诉谁发布,以及在什么时间等。显然有些事情不按计划进行..

# -*- 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,
        ),
    ]

3 个答案:

答案 0 :(得分:2)

我认为问题在于:

DateTimeField(default=0...)

使用Nonedatetime对象

答案 1 :(得分:1)

问题似乎与你正在使用的unicode方法有关。只需尝试使用str()并评论我的状态,如果它仍然导致错误。

欢呼: - )

答案 2 :(得分:0)

删除最后一个迁移文件。 然后替换model.DateField(null = True)上的日期字段。 可以迁移之后