django 1.8 不迁移日期时间字段
datetime fields not migrating with django 1.8
自升级到 django 1.8 以来,我的模型中的日期时间字段一直存在一些问题,无法正确迁移。
我反复看到这条消息:
Your models have changes that are not yet reflected in a migration, and so won't be applied.
Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
我 运行 makemigrations 我明白了:
operations = [
migrations.AlterField(
model_name='profile',
name='date_of_hire',
field=models.DateField(default=datetime.date(2016, 6, 5)),
),
]
所以我 运行 manage.py 迁移然后我得到:
Your models have changes that are not yet reflected in a migration, and so won't be applied.
Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
所以我 运行 再次进行迁移,我得到了一个与上面相同的新迁移。
这是我的问题字段:
date_of_hire = models.DateField(default=datetime.date.today())
查看迁移,我可以看到日期已明确设置为固定日期。所以现在如果我将字段更改为:
date_of_hire = models.DateField(auto_now_add=True)
或者这个:
date_of_hire = models.DateTimeField(auto_now_add=True)
我在尝试 运行 makemigrations 或启动我的服务器时遇到以下错误:
File "/urls.py", line 13, in <module>
import profiles.views as profile_views
File "/profiles/views.py", line 9, in <module>
from profiles.forms import CompanyProfileForm
File "/profiles/forms.py", line 19, in <module>
class ProfileForm(ModelForm):
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py", line 295, in __new__
raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (date_of_hire) specified for Profile
如果我在 forms.py 字段中注释掉该字段,则会列出除表单有效之外的所有内容。我可以进行迁移并应用它们,运行 服务器,等等,但是一旦我取消对该字段的注释,应用程序就会出现问题。所以我很茫然...
在您的 default
中,您应该传递可调用 datetime.date.today
,而不是调用它:
date_of_hire = models.DateField(default=datetime.date.today)
当您使用 default=datetime.date.today()
时,Django 会在您每次加载 models.py
时调用 today()
。这会更改默认值,因此 Django 认为需要进行新的迁移。
您必须再创建一个迁移才能将默认值更改为 datetime.date.today
(或编辑现有的迁移,但这会比较棘手)。
自升级到 django 1.8 以来,我的模型中的日期时间字段一直存在一些问题,无法正确迁移。
我反复看到这条消息:
Your models have changes that are not yet reflected in a migration, and so won't be applied.
Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
我 运行 makemigrations 我明白了:
operations = [
migrations.AlterField(
model_name='profile',
name='date_of_hire',
field=models.DateField(default=datetime.date(2016, 6, 5)),
),
]
所以我 运行 manage.py 迁移然后我得到:
Your models have changes that are not yet reflected in a migration, and so won't be applied.
Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
所以我 运行 再次进行迁移,我得到了一个与上面相同的新迁移。
这是我的问题字段:
date_of_hire = models.DateField(default=datetime.date.today())
查看迁移,我可以看到日期已明确设置为固定日期。所以现在如果我将字段更改为:
date_of_hire = models.DateField(auto_now_add=True)
或者这个:
date_of_hire = models.DateTimeField(auto_now_add=True)
我在尝试 运行 makemigrations 或启动我的服务器时遇到以下错误:
File "/urls.py", line 13, in <module>
import profiles.views as profile_views
File "/profiles/views.py", line 9, in <module>
from profiles.forms import CompanyProfileForm
File "/profiles/forms.py", line 19, in <module>
class ProfileForm(ModelForm):
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py", line 295, in __new__
raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (date_of_hire) specified for Profile
如果我在 forms.py 字段中注释掉该字段,则会列出除表单有效之外的所有内容。我可以进行迁移并应用它们,运行 服务器,等等,但是一旦我取消对该字段的注释,应用程序就会出现问题。所以我很茫然...
在您的 default
中,您应该传递可调用 datetime.date.today
,而不是调用它:
date_of_hire = models.DateField(default=datetime.date.today)
当您使用 default=datetime.date.today()
时,Django 会在您每次加载 models.py
时调用 today()
。这会更改默认值,因此 Django 认为需要进行新的迁移。
您必须再创建一个迁移才能将默认值更改为 datetime.date.today
(或编辑现有的迁移,但这会比较棘手)。