django 中的 DateTimeField 迁移错误

DateTimeField migration error in django

之前我的模型的 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)),
        ),
    ]

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

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

@Alasdair 的建议启发了我。

我做了什么来解决它:-

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

=>用 datetime.datetime.now 和 datetime.date.today

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

=> 完成,没有迁移建议或错误。