如何阻止模型迁移的无限循环,'模型具有尚未反映的变化' - makemigrations> migrate。同样的消息

问题描述 投票:0回答:1

Showmigrations:

accounts
 [X] 0001_initial
admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
 [X] 0003_logentry_add_action_flag_choices
auth

 [X] 0001_initial
 [X] 0002_auto_20190430_1129
blog
 [X] 0001_initial
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
core
 (no migrations)
curate
 [X] 0001_initial
 [X] 0002_item_tags
django_comments
 [X] 0001_initial
podcast
 [X] 0001_initial
 [X] 0002_auto_20190430_1129
 [X] 0003_auto_20190430_1132
sessions
 [X] 0001_initial
sites
 [X] 0001_initial
 [X] 0002_alter_domain_unique
taggit
 [X] 0001_initial

如果我运行迁移(只是为了看它是否说“没有要迁移的应用程序”)我得到这个:

Operations to perform:
  Apply all migrations: accounts, admin, auth, blog, contenttypes, curate, django_comments
, podcast, sessions, sites, taggit
Running migrations:
  No migrations to apply.
  Your models have changes that are not yet reflected in a migration, and so won't be appl
ied.
  Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrat
e' to apply them.

所以我运行makemigrations:

Migrations for 'podcast':
  podcast\migrations\0004_auto_20190430_1137.py
    - Alter field published_date on show

Showmigrations:

accounts
 [X] 0001_initial
admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
 [X] 0003_logentry_add_action_flag_choices
auth
 [X] 0001_initial

 [X] 0002_auto_20190430_1129
blog
 [X] 0001_initial
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
core
 (no migrations)
curate
 [X] 0001_initial
 [X] 0002_item_tags
django_comments
 [X] 0001_initial
podcast
 [X] 0001_initial
 [X] 0002_auto_20190430_1129
 [X] 0003_auto_20190430_1132
 [ ] 0004_auto_20190430_1137
sessions
 [X] 0001_initial
sites
 [X] 0001_initial
 [X] 0002_alter_domain_unique
taggit
 [X] 0001_initial

我再次运行迁移:

Operations to perform:
  Apply all migrations: accounts, admin, auth, blog, contenttypes, curate, django_comments
, podcast, sessions, sites, taggit
Running migrations:
C:\Users\phill\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\db\models\fields\__init
__.py:1421: RuntimeWarning: DateTimeField Show.published_date received a naive datetime (2
019-04-30 11:32:39.288026) while time zone support is active.
  RuntimeWarning)
C:\Users\phill\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\db\models\fields\__init
__.py:1421: RuntimeWarning: DateTimeField Show.published_date received a naive datetime (2
019-04-30 11:37:23.102936) while time zone support is active.
  RuntimeWarning)
  Applying podcast.0004_auto_20190430_1137... OK

但是,如果我再次运行迁移,我会收到相同的消息 - 这是一个循环。

Operations to perform:
  Apply all migrations: accounts, admin, auth, blog, contenttypes, curate, django_comments
, podcast, sessions, sites, taggit
Running migrations:
  No migrations to apply.
  Your models have changes that are not yet reflected in a migration, and so won't be appl
ied.
  Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrat
e' to apply them.

makemigrations:

Migrations for 'podcast':
  podcast\migrations\0005_auto_20190430_1139.py
    - Alter field published_date on show

这是生成运行时错误的模型中的行,我提供了一个默认值,因为它是现有模型的新属性,需要一个。 Python也促使我使用这个,也许这是错误的做法?

published_date = models.DateTimeField(_('Date published'), default=datetime.datetime.today(), null=True, blank=True, help_text=_('The date the feed was published'))

循环迁移的迁移代码如下:

import datetime
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('podcast', '0003_auto_20190430_1132'),
    ]

    operations = [
        migrations.AlterField(
            model_name='show',
            name='published_date',
            field=models.DateTimeField(blank=True, default=datetime.datetime(2019, 4, 30, 11, 37, 23, 102936), help_text='The date the feed was published', null=True, verbose_name='Date published'),
        ),
    ]

python django model migration
1个回答
1
投票

你需要传递datetime.date.today的callable,而不是调用它的结果,作为默认值。由于结果每次都会改变,Django会认为你已经改变了默认值。

published_date = models.DateTimeField(_('Date published'), default=datetime.datetime.today, null=True, blank=True, help_text=_('The date the feed was published'))
#                                                                                         ^ no parens
© www.soinside.com 2019 - 2024. All rights reserved.