Issue
I'm not sure if this has been answered before but here goes
I have this code
ITEM = [
('Fruits', (
('a', 'Apple'),
('g', 'Grape'),
('m', 'Mango')
)
),
('Vegetables', (
('b', 'Broccoli'),
('p', 'Peanut'),
('t', 'Tomato')
)
),
]
With that I'm trying to access the human-readable version of the tuples in my model like this
item = models.CharField(max_length=50, choices=ITEM)
However when I submitted this field from django admin page, it submits let's say 'a' instead of 'Apple'
How can I make sure that I submitted 'Apple' into the database to be displayed in HTML later on?
Solution
You can render the value with get_fieldname_display
[Django-doc]. As is specified in the documentation:
For every field that has choices set, the object will have a
get_FOO_display()
method, whereFOO
is the name of the field. This method returns the "human-readable" value of the field.
So if you later in your template want to render the human-readable value for the item
field, you render this with:
{{ my_object.get_item_display }}
Answered By - willeM_ Van Onsem
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.