【发布时间】: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