使用 Enum 时管理员中的 Django 选择

Django choices in Admin when Enum is used

我有一个模型,我在其中使用枚举进行选择:

class Agreement(models.Model):
    class Category(enum.Enum):
        EULA = 0
        PROVIDER = 1

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    category = models.IntegerField(
        choices=[(choice.name, choice.value)
                 for choice in Category])
    title = models.CharField(max_length=128)
    content = models.TextField()

我使用简单的管理站点注册来注册它:

admin.site.register(Agreement)

当管理站点呈现对象时它不允许我保存它?有人遇到过类似的问题吗?

根据 documentation:

The first element in each tuple is the actual value to be set on the model, and the second element is the human-readable name.

namevalue 应该反过来,像这样:

category = models.IntegerField(
    choices=[(choice.value, choice.name)
             for choice in Category])

因为 category 是一个整数字段而 name returns 是一个字符串。