I have a model class of which I want two fields to be a choice fields, so to populate those choices I am using an enum as listed below
#models.py
class Transaction(models.Model):
trasaction_status = models.CharField(max_length=255, choices=TransactionStatus.choices())
transaction_type = models.CharField(max_length=255, choices=TransactionType.choices())
#enums.py
class TransactionType(Enum):
IN = "IN",
OUT = "OUT"
@classmethod
def choices(cls):
print(tuple((i.name, i.value) for i in cls))
return tuple((i.name, i.value) for i in cls)
class TransactionStatus(Enum):
INITIATED = "INITIATED",
PENDING = "PENDING",
COMPLETED = "COMPLETED",
FAILED = "FAILED"
ERROR = "ERROR"
@classmethod
def choices(cls):
print(tuple((i.name, i.value) for i in cls))
return tuple((i.name, i.value) for i in cls)
However, when I am trying to access this model through admin I am getting the following error :
Django Version: 1.11
Exception Type: ValueError
Exception Value:
too many values to unpack (expected 2)
I followed two articles that described how to use enums:
- https://hackernoon.com/using-enum-as-model-field-choice-in-django-92d8b97aaa63
- https://blog.richard.do/2014/02/18/how-to-use-enums-for-django-field-choices/