django中的DateTimeField迁移错误

时间:2017-07-28 08:31:41

标签: django python-2.7 datetime migration django-1.8

以前我的模型'DateTimeField就像

一样启动了
order_date = models.DateTimeField(default=datetime.today)

投掷

TypeError: Required argument 'year' (pos 1) not found

当我试图实例化一个空白对象时。 我用datetime.datetime.now()修复了它。 但是现在当我尝试迁移它时显示相同的错误

TypeError: Required argument 'year' (pos 1) not found

当我在一个新的数据库中清理迁移它没有任何问题。 我不想继续制作一个新的数据库,因为我没有权限在生产中这样做。有什么方法可以解决这个问题吗?

回溯:

  Running migrations:
  Rendering model states... DONE
  Applying xyz.0016_auto_20170728_0758...Traceback (most recent call last):
  File "manage.py", line 9, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/sayok/projects/invenv1/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/Users/sayok/projects/invenv1/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/sayok/projects/invenv1/lib/python2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/sayok/projects/invenv1/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "/Users/sayok/projects/invenv1/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 221, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/Users/sayok/projects/invenv1/lib/python2.7/site-packages/django/db/migrations/executor.py", line 110, in migrate
    self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
  File "/Users/sayok/projects/invenv1/lib/python2.7/site-packages/django/db/migrations/executor.py", line 147, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/Users/sayok/projects/invenv1/lib/python2.7/site-packages/django/db/migrations/migration.py", line 115, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/Users/sayok/projects/invenv1/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 201, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/Users/sayok/projects/invenv1/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 484, in alter_field
    old_db_params, new_db_params, strict)
  File "/Users/sayok/projects/invenv1/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 567, in _alter_field
    old_default = self.effective_default(old_field)
  File "/Users/sayok/projects/invenv1/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 194, in effective_default
    default = field.get_default()
  File "/Users/sayok/projects/invenv1/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 794, in get_default
    return self.default()
TypeError: Required argument 'year' (pos 1) not found

迁移文件

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

from django.db import models, migrations
import datetime


class Migration(migrations.Migration):
    dependencies = [
        ('p1', '0015_vgvjvvhvvj'),
    ]

    operations = [
        migrations.AlterField(
            model_name='analyze',
            name='created_date',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 909746)),
        ),
        migrations.AlterField(
            model_name='apiplaceorder',
            name='order_date',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 972820)),
        ),
        migrations.AlterField(
            model_name='hjjghjg',
            name='order_date',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 980004)),
        ),
        migrations.AlterField(
            model_name='banner',
            name='created_date',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 932895)),
        ),
        migrations.AlterField(
            model_name='clusterarticle',
            name='created_date',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 975352)),
        ),
        migrations.AlterField(
            model_name='clusterlog',
            name='updated_date',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 885144)),
        ),
        migrations.AlterField(
            model_name='customize',
            name='created_date',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 906463)),
        ),
        migrations.AlterField(
            model_name='faqtopic',
            name='created_date',

        migrations.AlterField(
            model_name='greetingstouser',
            name='created_date',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 963740)),
        ),
        migrations.AlterField(
            model_name='kycsendemailtouser',
            name='send_date',
            field=models.DateField(default=datetime.date(2017, 7, 28)),
        ),
        migrations.AlterField(
            model_name='liveorder',
            name='order_date',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 971116)),
        ),
        migrations.AlterField(
            model_name='hgjgjgjgjg',
            name='last_login',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 976968)),
        ),
        migrations.AlterField(
            model_name='newsletter',
            name='created_date',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 940914)),
        ),
        migrations.AlterField(
            model_name='newslettertouser',
            name='created_date',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 942440)),
        ),
        migrations.AlterField(
            model_name='newsroom',
            name='created_date',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 944774)),
        ),
        migrations.AlterField(
            model_name='optimize',
            name='created_date',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 910911)),
        ),
        migrations.AlterField(
            model_name='order',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 910911)),
        ),
        migrations.AlterField(
            model_name='order',
            name='order_date',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 913976)),
        ),
        migrations.AlterField(
            model_name='orderaccountgraph',
            name='order_date',
            field=models.DateField(default=datetime.date(2017, 7, 28)),
        ),
        migrations.AlterField(
            model_name='orderbuy',
            name='order_date',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 918101)),
        ),
        migrations.AlterField(
            model_name='orderhold',
            name='hold_date',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 924012)),
        ),
        migrations.AlterField(
            model_name='ordersell',
            name='sell_date',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 921352)),
        ),
        migrations.AlterField(
            model_name='ordertrack',
            name='order_date',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 931269)),
        ),
        migrations.AlterField(
            model_name='partner',
            name='created_date',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 904619)),
        ),
        migrations.AlterField(
            model_name='recentlyviewedcluster',
            name='viewed_date',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 911741)),
        ),
        migrations.AlterField(
            model_name='seasongreetings',

        migrations.AlterField(
            model_name='seasongreetings',
            name='created_date',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 962918)),
        ),
        migrations.AlterField(
            model_name='sitesetting',
            name='created_date',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 907550)),
        ),
        migrations.AlterField(
            model_name='subscribe',
            name='created_date',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 940109)),
        ),
        migrations.AlterField(
            model_name='team',
            name='created_date',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 905406)),
        ),
        migrations.AlterField(
            model_name='testimonial',
            name='created_date',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 903870)),
        ),
        migrations.AlterField(
            model_name='user',
            name='created_date',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 891457)),
        ),
        migrations.AlterField(
            model_name='usernotification',
            name='notification_date',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 912805)),
        ),
        migrations.AlterField(
            model_name='vghgh',
            name='application_date',
            field=models.DateTimeField(default=datetime.datetime(2017, 7, 28, 7, 58, 21, 957282)),
        ),
    ]

2 个答案:

答案 0 :(得分:1)

回溯中的这一行表明您在之前的迁移中有一个不正确的默认值。

old_default = self.effective_default(old_field)

检查应用中的旧迁移,然后查找default=datetime.datetimedefault=datetime.date。这些应该是default=datetime.datetime.todaydatetime.date.today

顺便说一句,你不应该使用default=datetime.now()。 Django在您创建迁移时调用该函数,并在datetime(2017, 7, 28, 7, 58, 21)冻结它。当您运行迁移时,它将使用该日期时间,即使您从现在开始运行很多天。请改用可调用的datetime.now

答案 1 :(得分:0)

我被@ Alasdair的建议所启发。

我做了什么来解决它: -

=&gt;将所有默认值更新为datetime.datetime.now和datetime.date.today

=&gt;用datetime.datetime.now和datetime.date.today

替换了我的0001_initial(与您相关的)默认值

=&GT;完成,没有迁移建议或错误。