在 Django 选择字段中一起使用整数和字符串

Using integers and strings together in Django choice field

我正在尝试将最新 Django 版本中的新枚举类型用于选择字段。具体来说,我试图按如下方式存储美国的各个州:

class States(models.TextChoices):
    ALABAMA = 'AL', 'Alabama'
    ALASKA = 'AK', 'Alaska'
    .....
    .....
    WISCONSIN = 'WI', 'Wisconsin'
    WYOMING = 'WY', 'Wyoming'


class PersonalInfo(models.Model):
    state = models.CharField(max_length=2, choices=States.choices, default=States.ALABAMA)

按预期工作。 现在,我还试图使 max_length 变量也成为选择 class 的 class 属性,通过执行以下操作使代码更加模块化:

class States(models.TextChoices):
    ALABAMA = 'AL', 'Alabama'
    ALASKA = 'AK', 'Alaska'
    .....
    .....
    WISCONSIN = 'WI', 'Wisconsin'
    WYOMING = 'WY', 'Wyoming'
    MAX_LENGTH = 2
    

class PersonalInfo(models.Model):
    state = models.CharField(max_length=States.MAX_LENGTH, choices=States.choices, default=States.ALABAMA)

这给我一个错误如下:

if self.max_length is not None and choice_max_length > self.max_length:
TypeError: '>' not supported between instances of 'int' and 'States'

我知道 Django 还为整数提供了替代 IntegerChoices,但我如何同时使用文本和整数选择。

TextChoices 必须包含字符串值,它们的工作方式是枚举您在 class 中定义的内容。因此,您混合了多种类型,这些类型将不起作用,因为它会尝试将 2 作为选择之一,因为它是选择 class.

的一部分

您可以将选项定义为常量,有点像这样;

    STATE_MAX_LENGTH = 2


    class States(models.TextChoices):
        ALABAMA = 'AL', 'Alabama'
        ALASKA = 'AK', 'Alaska'
        .....
        .....
        WISCONSIN = 'WI', 'Wisconsin'
        WYOMING = 'WY', 'Wyoming'
        

    class PersonalInfo(models.Model):
        state = models.CharField(max_length=STATE_MAX_LENGTH, choices=States.choices, default=States.ALABAMA)

确认一下,这里是来自 django 的 classes 选项;

class Choices(enum.Enum, metaclass=ChoicesMeta):
    """Class for creating enumerated choices."""

    def __str__(self):
        """
        Use value when cast to str, so that Choices set as model instance
        attributes are rendered as expected in templates and similar contexts.
        """
        return str(self.value)


class IntegerChoices(int, Choices):
    """Class for creating enumerated integer choices."""
    pass


class TextChoices(str, Choices):
    """Class for creating enumerated string choices."""

    def _generate_next_value_(name, start, count, last_values):
        return name