如何从Django选择字段中的值中检索键? [英] How do I retrieve key from value in django choices field?
本文介绍了如何从Django选择字段中的值中检索键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
示例代码如下:
REFUND_STATUS = (
('S', 'SUCCESS'),
('F', 'FAIL')
)
refund_status = models.CharField(max_length=3, choices=REFUND_STATUS)
我知道在模型中我可以使用get_refund_status_display()方法检索成功。但是,如果要反向执行以下操作:我有成功,我想找到缩写形式 S。
I know in the model I can retrieve the SUCCESS with method get_refund_status_display() method. However, if I want to do it reversely like: I have 'SUCCESS' I want to find the abbreviated form 'S'. How can I do that in django or python?
推荐答案
将其转换为字典。
refund_dict = {value: key for key, value in REFUND_STATUS}
actual_status = refund_dict[display_status]
这篇关于如何从Django选择字段中的值中检索键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文