按照django official documentation for Django3.2 的一种更简单的方法
class Transaction(models.Model):
class TransactionTypes(models.IntegerChoices):
BUY = 0, _('Buy')
SELL = 1, _('Sell')
(或)
class Transaction(models.Model):
class TransactionTypes(models.IntegerChoices):
BUY = 0, 'Buy'
SELL = 1, 'Sell'
另一种方法是利用Enum functional api,这在Django 3.2官方文档中也有提及
TransactionTypes = models.IntegerChoices('TransactionTypes', 'BUY SELL')
TransactionTypes.choices
#provides below output
>>>[(1, 'Buy'), (2, 'Sell')]
编辑:1
考虑到您只有少数交易类型(如买入/卖出和其他未来交易类型可能性,如交换或退货),我建议使用更适合您的场景的PositiveSmallIntegerField。
此处PositiveSmallIntegerField 支持从 0 到 32767 的值,而 SmallIntegerField 支持从 -32768 到 32767 的值
语法:
models.PositiveSmallIntegerField(**Field Options)
例子:
class Transaction(models.Model):
class TransactionTypes(models.IntegerChoices):
BUY = 0, 'Buy'
SELL = 1, 'Sell'
start_transactionType= models.PositiveSmallIntegerField(choices=TransactionTypes.choices, default=TransactionTypes.BUY, help_text="Do you wish to Buy or Sell?", null=True, blank=True, primary_key=False, editable=True)
def __str__(self):
return '%s' % (self.start_transactionType)
__ str __ 是一种 Python 方法,它返回任何对象的字符串表示形式。这是 Django 用来将模型实例显示为纯字符串的方法。
字段选项
-
choices :设置该字段的选项
-
default:字段的默认值
-
help_text:与表单小部件一起显示的额外“帮助”文本。即使您的字段未在表单上使用,它对文档也很有用
-
null:如果设置为True,Django 将空值作为NULL 存储在数据库中,默认为False。
-
blank:如果是True,该字段允许为空,默认为False
-
primary_key:如果是True,这个字段是模型的主键,默认是False
-
editable:如果False,该字段将不会显示在管理员或任何其他模型窗体中。在模型验证期间它们也会被跳过。默认为 True。
对于一个实时示例,您可以按照这个 5 部分教程系列,
part 5: Fluent in Django: Get to know Django models better
编辑:2
枚举类中添加了许多自定义属性(.choices、.labels、.values 和 .names),以便更轻松地访问枚举中这些单独部分的列表。
根据 django 文档,可以使用 .label 属性或 .name 属性
TransactionTypes.BUY.label
>>> “Buy” #returns this output as string value
TransactionType.BUY.name
>>> “BUY” # returns this output
TransactionType.BUY.value
>>> 0 # returns this as output
EDIT 3基于更新的问题&cmets
编辑 3 中包含的简要信息
-
额外的实例方法引用自 django 3.2 doc 的示例
- 如何将额外的实例方法应用于您的用例
- 解决问题的变通功能
Django 3.2 documentation on extra instance method 提及
对于每个设置了选项的字段,对象将有一个 get_FOO_display() 方法,其中 FOO 是字段的名称。此方法返回字段的“人类可读”值。
下面给出了文档中的示例示例
from django.db import models
class Person(models.Model):
SHIRT_SIZES = (
('S', 'Small'),
('M', 'Medium'),
('L', 'Large'),
)
name = models.CharField(max_length=60)
shirt_size = models.CharField(max_length=2, choices=SHIRT_SIZES)
>>>p = Person(name="Fred Flintstone", shirt_size="L")
>>>p.save()
>>>p.shirt_size
‘L’ #output
>>> p.get_shirt_size_display()
‘Large’ #output
将额外的实例方法应用到您的用例
根据您更新的问题和 cmets,您提到 t 是 Transactions 对象的实例,type 是 PositiveSmallIntegerField(TransactionTypes 选择的实例)
t.get_type_display() 代码理想情况下应该将输出Buy 生成为字符串
>>> type= models.PositiveSmallIntegersField(choices=TransactionTypes.choices, null=True, blank=True)
>>> t = Transaction.objects.all().first()
>>> t.type
0 #output
>>> t.get_type_display()
‘Buy’ #output
解决方法
一种解决方法是编写一个单独的函数来检查 int 枚举值并将标签作为字符串返回
def label_of_TransactionType:
if (t.type== TransactionType.BUY.value):
return TransactionTypes.BUY.label
else:
return TransactionTypes.SELL.label