How do I retrieve key from value in django choices field?

2024/9/18 20:06:35

The sample code is below:

REFUND_STATUS = (('S', 'SUCCESS'),('F', 'FAIL')
)
refund_status = models.CharField(max_length=3, choices=REFUND_STATUS)

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?

Answer

Convert it to a dict.

refund_dict = {value: key for key, value in REFUND_STATUS}
actual_status = refund_dict[display_status]
https://en.xdnf.cn/q/72859.html

Related Q&A

Errno13, Permission denied when trying to read file

I have created a small python script. With that I am trying to read a txt file but my access is denied resolving to an no.13 error, here is my code:import time import osdestPath = C:\Users\PC\Desktop\N…

How to write large JSON data?

I have been trying to write large amount (>800mb) of data to JSON file; I did some fair amount of trial and error to get this code:def write_to_cube(data):with open(test.json) as file1:temp_data = j…

Using absolute_import and handling relative module name conflicts in python [duplicate]

This question already has answers here:How can I import from the standard library, when my project has a module with the same name? (How can I control where Python looks for modules?)(7 answers)Close…

Setting results of torch.gather(...) calls

I have a 2D pytorch tensor of shape n by m. I want to index the second dimension using a list of indices (which could be done with torch.gather) then then also set new values to the result of the index…

Why does PyCharm use double backslash to indicate escaping?

For instance, I write a normal string and another "abnormal" string like this:Now I debug it, finding that in the debug tool, the "abnormal" string will be shown like this:Heres the…

Execute Shell Script from Python with multiple pipes

I want to execute the following Shell Command in a python script:dom=myserver cat /etc/xen/$myserver.cfg | grep limited | cut -d= -f2 | tr -d \"I have this:dom = myserverlimit = subprocess.cal…

Python: Is there a way to split a string of numbers into every 3rd number?

For example, if I have a string a=123456789876567543 could i have a list like...123 456 789 876 567 543

How to override the __dir__ method for a class?

I want to change the dir() output for my class. Normally, for all other objects, its done by defining own __dir__ method in their class. But if I do this for my class, its not called.class X(object):de…

Equivalent of wget in Python to download website and resources

Same thing asked 2.5 years ago in Downloading a web page and all of its resource files in Python but doesnt lead to an answer and the please see related topic isnt really asking the same thing.I want t…

Python plt: close or clear figure does not work

I generate a lots of figures with a script which I do not display but store to harddrive. After a while I get the message/usr/lib/pymodules/python2.7/matplotlib/pyplot.py:412: RuntimeWarning: More than…