Django 自动改变驼峰大小写
Django automatically changes camel case
我在 Django 中使用 models.IntegerChoices 作为枚举,但它以更改的方式保存在数据库中。
class FruitsEnum(models.IntegerChoices):
Apple = 1
RedApple = 2
GreenApple = 3
LongBanana = 4
DragonFruit = 5
但它是这样保存在数据库中的:[('0','Apple'),('1','Redapple'),('2','Greenapple') ...]
如您所见,单词 'apple' 在两个单词组合中不是大写。我怎样才能做到这一点:
[('0','Apple'),('1','RedApple'),('2','GreenApple')...]
不传递整数,只传递一个由整数和相关名称组成的元组。类似于:
class FruitsEnum(models.IntegerChoices):
Apple = 1, 'Apple'
RedApple = 2, 'RedApple'
GreenApple = 3, 'GreenApple'
LongBanana = 4, 'LongBanana'
DragonFruit = 5, 'DragonFruit'
或者,在您的模型中,在您使用它的 IntegerField
中,您可以将 FruitsEnum.choices
替换为如下元组:
[(1,'Apple'),(2,'RedApple'),(3,'GreenApple'),(4,'LongBanana'),(5,'DragonFruit')]
注意: 您在此处观察到的任何差异纯粹是装饰性的,不存在于 django 之外(即在数据库中)。直接打开数据库可以看到table.
里面只存了整数
我在 Django 中使用 models.IntegerChoices 作为枚举,但它以更改的方式保存在数据库中。
class FruitsEnum(models.IntegerChoices):
Apple = 1
RedApple = 2
GreenApple = 3
LongBanana = 4
DragonFruit = 5
但它是这样保存在数据库中的:[('0','Apple'),('1','Redapple'),('2','Greenapple') ...]
如您所见,单词 'apple' 在两个单词组合中不是大写。我怎样才能做到这一点: [('0','Apple'),('1','RedApple'),('2','GreenApple')...]
不传递整数,只传递一个由整数和相关名称组成的元组。类似于:
class FruitsEnum(models.IntegerChoices):
Apple = 1, 'Apple'
RedApple = 2, 'RedApple'
GreenApple = 3, 'GreenApple'
LongBanana = 4, 'LongBanana'
DragonFruit = 5, 'DragonFruit'
或者,在您的模型中,在您使用它的 IntegerField
中,您可以将 FruitsEnum.choices
替换为如下元组:
[(1,'Apple'),(2,'RedApple'),(3,'GreenApple'),(4,'LongBanana'),(5,'DragonFruit')]
注意: 您在此处观察到的任何差异纯粹是装饰性的,不存在于 django 之外(即在数据库中)。直接打开数据库可以看到table.
里面只存了整数