【问题标题】:How to set default value for a model field based on enum in Django?如何在Django中基于枚举为模型字段设置默认值?
【发布时间】:2021-03-26 06:02:44
【问题描述】:

我正在使用 Django 2.2.5,并且在我的模型中具有基于枚举的多项选择字段。由于未知原因,我现在在迁移期间使用枚举作为选择字段时遇到迁移错误:

django.db.utils.OperationalError: (1067, "Invalid default value for 'protocol'")

model.py

from django.db import models
# See class above
from .utils import NetworkProtocolList

class Networks(models.Model):
    ipv4 = models.GenericIPAddressField(blank=False, null=False)
    protocol = models.CharField(choices=NetworkProtocolList.choices(), max_length=20,default=NetworkProtocolList.ETH)

    class Meta:
        managed = True
        db_table = 'networks'

utils.py

from enum import Enum

class NetworkProtocolList(Enum):
    ETH = 'Ethernet'
    MPLS = 'MPLS' 
   
    @classmethod
    def choices(cls):
        return [(key.name, key.value) for key in cls]  

我发了

manage.py 迁移

及后续

manage.py 迁移

产生以下错误:

django.db.utils.OperationalError: (1067, "默认值无效 '协议'")

xxxx_auto_xxxxxxxx_xxxx.py

# Auto generated migration file

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

class Migration(migrations.Migration):

    dependencies = [
        ('my_app', 'yyyy_auto_yyyyyyyy_yyyy'),
    ]

    operations = [
        migrations.AddField(
            model_name='networks',
            name='protocol',
            # Field definition here, pay attention to the default value
            field=models.CharField(choices=[('ETH', 'Ethernet'), ('MPLS', 'MPLS')], default=my_app.utils.NetworkProtocolList('Ethernet'), max_length=20),
        ),
    ]

比我编辑迁移文件手动将默认设置为字符串而不是调用枚举类:

xxxx_auto_xxxxxxxx_xxxx.py

# Edited migration file

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

class Migration(migrations.Migration):

    dependencies = [
        ('my_app', 'yyyy_auto_yyyyyyyy_yyyy'),
    ]

    operations = [
        migrations.AddField(
            model_name='networks',
            name='protocol',
            # Field definition here, pay attention to the modified default value
            field=models.CharField(choices=[('ETH', 'Ethernet'), ('MPLS', 'MPLS')], default='Ethernet', max_length=20),
        ),
    ]

现在迁移可以正常工作,但我想知道为什么我不能像使用枚举而不是 litteral 字符串之前那样定义默认值,因为我有其他模型字段可以正常工作。

这是一个错误,我在这里说什么,如何在 Django 中基于枚举为模型字段设置默认值?

【问题讨论】:

    标签: django django-models django-migrations


    【解决方案1】:

    由于您使用的是 Django.value 属性作为

    protocol = models.CharField(
        choices=NetworkProtocolList.choices(),
        max_length=20,
        default=NetworkProtocolList.ETH.value # &lt--- change is here
    )

    【讨论】:

    • 谢谢,我会在不久的将来为枚举字段执行此操作。但这很奇怪,因为我已经有多个字段具有提到的语法 (default=MyList.KEY_NAME),而且我不记得遇到过迁移错误,为什么迁移现在而不是之前会产生错误?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 2015-09-26
    • 1970-01-01
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多