[英]django adding extra data to IntegerChoices
I need to add extra data to enum Class IntegerChoice in django. I have carefully read the answer here How to add extra data to TextChoices?我需要向 django 中的 enum Class IntegerChoice 添加额外的数据。我已经仔细阅读了这里的答案How to add extra data to TextChoices? but it does not seem to work.
但它似乎不起作用。 the definition of my enum type is:
我的枚举类型的定义是:
class FabricType (models.IntegerChoices):
"""an enumeration class with type, subclass and key words added
first item is the code,
second is translated verbose name,
third is choice specific help,
third is Group
fourth is a list of keywords
anychange in the number will require migration but adding new keywords will not
"""
CASTON = (1, _('Caston'), _('Basic'), [])
JERSEY = (2, _('Jersey'), _('Basic'), [])
TUBULAR = (3, _('Tubular'), _('Basic'), [])
def __new__(cls, value, label, group, keywords, ):
obj = int.__new__(cls, value)
obj._value_ = value
obj.group = group
obj.keywords = keywords
obj.help = help
return obj
def help_dict(cls):
"""help_dict for the FabricType"""
help_dict = {'CASTON': _('the first few rows of a knit'),
'JERSEY': _('help_text'),
'TUBULAR': _('help text')
return help_dict
I have declared the EnumMixin as follows:我已经声明 EnumMixin 如下:
class EnumMixin:
''' an Enum which Converts a DB value back to its Choices value'''
def __init__(self, *args, enum=models.Choices, **kwargs):
self.__enum = enum
self.help_dict = kwargs.get('help_dict', None)
kwargs.pop('help_dict', None)
# it sets choices for you using the enum
super().__init__(*args, **kwargs)
def deconstruct(self):
'Get constructor args to reconstruct this field with later'
name, path, args, kwargs = super().deconstruct()
kwargs['enum'] = self.__enum
kwargs['help_dict'] = self.help_dict
try:
del kwargs['choices']
except KeyError:
pass
return name, path, args, kwargs
def from_db_value(self, value, expression, connection):
# Convert from db value
return self.__to_enum(value)
def to_python(self, value):
'Called by deserialization and during clean() method used in forms'
return self.__to_enum(value)
def __to_enum(self, value):
if value is None:
return None
return self.__enum(value)
def help_dict(self):
return
my field is declared as follows:我的字段声明如下:
class FabricTypeField(EnumMixin, models.SmallIntegerField):
pass
When I create an object in the database with value 2, I get the following error: builtins.ValueError: 2 is not a valid Choices.当我在数据库中创建值为 2 的 object 时,出现以下错误:builtins.ValueError: 2 不是有效的选择。
in the EnumMixin class __to_enum(self, value) is called but self.__enum(value) ends up in the Python EnumMeta(type) and not in my enum class.在 EnumMixin class 中调用了 __to_enum(self, value) 但 self.__enum(value) 最终出现在 Python EnumMeta(type) 中,而不是在我的枚举 class 中。
The error came from the declaration of the field in the model where I forgot to mention the type of the enum错误来自 model 中的字段声明,我忘记提及枚举的类型
class Fabric(models.Model) :
fabric_type = FabricTypeField(
**enum=FabricType,**
verbose_name=_('fabric_type'),
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.