开发者

Django enumaration problem

开发者 https://www.devze.com 2023-03-18 00:35 出处:网络
I try to implement enumartion structure in django such that class Status(): PENDING = 0 CONFIRMED = 1 DENIED = 2

I try to implement enumartion structure in django such that

class Status():
PENDING = 0
CONFIRMED = 1
DENIED = 2

STATUS =(
    (PENDING,_("salary_status_pending")),
    (CONFIRMED,_("salary_status_confirmed")),
    (DENIED,_("salary_status_denied")),
)

and in my model I use it like

class MyModel(models.Model):
    status = models.IntegerField(null=False, choices=Status.STATUS)

It works fine and if I want to get the label of enum field in my template I use {{ mymodel.get_status_display }} and it writes the label _('key..') in my enum field instead of number which is explained in Django documents

However, what if I want to get the label in my view.py ? I want to write a code below and it should give me the label of enum field instead of number

Status.CONFI开发者_如何学PythonRMED 

How can I achive this ?

Thanks


How about this?

class MyModel(models.Model):
    PENDING = 0
    CONFIRMED = 1
    DENIED = 2

    STATUS = {PENDING:_("salary_status_pending"), CONFIRMED:..., DENIED:... }    
    STATUS_CHOICES = [(a,a) for a in STATUS.items()]    
    status = models.IntegerField(null=False, choices=STATUS_CHOICES)

And you can access your label everywhere using MyModel.STATUS[MyModel.CONFIRMED]


I found it.The code below does what I want

Status.STATUS [Status.CONFIRMED ][1]
0

精彩评论

暂无评论...
验证码 换一张
取 消