[BangPypers] Getting label of the choicefield in django form

Gora Mohanty gora at mimirtech.com
Sat Oct 22 11:37:26 CEST 2011


On Sat, Oct 22, 2011 at 1:32 PM, Asif Jamadar <asif.jamadar at rezayat.net> wrote:
> Can you explain this with example so that I can understand?

GENDERS = (
    ('U', 'Unspecified',),
    ('M', 'Male',),
    ('F', 'Female'),
 )

Now, say you have a
   gender = forms.ChoiceField( label='Gender', choices=GROUPS )
after validating a form containing this, you might get
   gender_in = form.cleaned_data['gender']
to be equal to 'M'. If I understood you correctly, you would want
to get 'Male' from this. As you know that the choices of the form
field came from choices, the straightforward way to do this
would be:
    for g in GENDERS:
        if g[0] == gender_in:
            gender_out = g[1]
gender_out now has what you need.

Regards,
Gora


More information about the BangPypers mailing list