改变了 Django 模型。我为 DateField 提供了一次性的 default = datetime.date。发现无效。去掉了字段还是不能迁移

新手上路,请多包涵

您好,这是我在 models.py 中添加的特定行

billingMonth2 = models.DateTimeField()

这就是 makemigrations 中发生的事情,我想我添加了一个不可为 null 的字段,所以我尝试定义一个默认值:

     You are trying to add a non-nullable field 'billingMonth' to bills
    without a default; we can't do that (the database needs something to populate
    existing rows).
    Please select a fix:
    1) Provide a one-off default now (will be set on all existing rows)
    2) Quit, and let me add a default in models.py
    Select an option: 1
    Please enter the default value now, as valid Python
    The datetime and django.utils.timezone modules are available, so you can do
    e.g. timezone.now()
    >>> datetime.date
    Migrations for 'bills':
    0007_auto_20160609_1400.py:
    - Change Meta options on bills
    - Add field billingMonth to bills

我发现 datetime.date 无效并出现错误

    ....File "/home/jrbenriquez/Envs/env1/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 197, in effective_default
default = field.get_default()
    File "/home/jrbenriquez/Envs/env1/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 797, in get_default
return self.default()
    TypeError: Required argument 'year' (pos 1) not found

所以我只是删除了 billingMonth2 字段,希望它会恢复正常,并将默认值设置为

    `billingMonth2 = models.DateTimeField(default='Test',null=True,blank=True)`

两者仍然给出相同的错误。在我看来,我已经在其他文件或数据库中更改了模型元数据中的某些内容,并且我必须在某处删除默认值“datetime.date”

我希望有人能在这里帮助我。谢谢!

更新:这是 0007_auto20160609_1400.py

     # -*- coding: utf-8 -*-
    # Generated by Django 1.9.6 on 2016-06-09 14:00
    from __future__ import unicode_literals

    from django.db import migrations, models
    import django.utils.datetime_safe

    class Migration(migrations.Migration):

        dependencies = [
            ('bills', '0006_bills_duedate'),
        ]

        operations = [
            migrations.AlterModelOptions(
                name='bills',
                options={'ordering': ('-id', '-status'),     'verbose_name_plural': 'Bills'},
             ),
            migrations.AddField(
                model_name='bills',
                name='billingMonth',
                field=models.DateField(default=django.utils.datetime_safe.date),
                preserve_default=False,
             ),
]

原文由 JR Enriquez 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 839
2 个回答
Please enter the default value now, as valid Python
The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now()
>>> datetime.date

我猜它是在询问方法的返回值,而不是方法本身。所以它的 datetime.date() 最后是 ()

此外,在模型的字段声明中,您可以将当前日期设置为默认日期,如果这是您尝试做的

billingMonth2 = models.DateTimeField(default=datetime.date, null=True, blank=True)

在那里你给它方法本身,没有 () ,所以时间是在创建新行时花费的,而不仅仅是在首次定义模型时。

原文由 C14L 发布,翻译遵循 CC BY-SA 3.0 许可协议

签出这张图片:

签出这张图片

尝试以下操作。注意: 您将丢失数据库中的所有数据!

  1. 删除应用程序中的所有迁移(例如:001_initial.py)
  2. 删除数据库(对我来说是db.sqlite3,它是django提供的)(如果有外部数据库,删除数据库并创建新的)
  3. Atlast 运行:python manage.py makemigrations
  4. Python 管理.py 迁移

之后就可以正常运行了。

希望它会有所帮助。

原文由 Fazil Sha 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题