Ошибка миграции DateTimeField в 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)),
),
]
2 ответа
Эта строка в трассировке указывает, что в предыдущей миграции вы использовали неверное значение по умолчанию.
old_default = self.effective_default(old_field)
Проверьте свои старые миграции в приложении и найдите default=datetime.datetime
или же default=datetime.date
, Это должно быть default=datetime.datetime.today
или же datetime.date.today
вместо.
Кроме того, вы не должны использовать default=datetime.now()
, Django вызывает функцию при создании миграции и замораживает ее в datetime(2017, 7, 28, 7, 58, 21)
, Когда вы запустите миграцию, она будет использовать эту дату и время, даже если вы запустите ее через много дней. Используйте вызываемый datetime.now
вместо.
Я был просветлен предложением @Alasdair.
Что я сделал, чтобы решить это: -
=> Обновить все значения по умолчанию на datetime.datetime.now и datetime.date.today
=> заменил мои значения 0001_initial(в зависимости от того, что вам подходит) по умолчанию на datetime.datetime.now и datetime.date.today
=> Готово, нет предложений по миграции или ошибок.