How do I get the choices field values and not the key from the form?
I have a form where I let the user select some user's emails for a company. For example I have a form like this (this reason for model form is that it's inside a formset - but that is not important for now):
class Contacts(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(Contacts, self).__init__(*args, **kwargs)
self.company = kwargs['initial']['company']
self.fields['emails'].choices = self.company.emails
# This produces stuff like:
# [(1, '[email protected]'), ...]
emails = forms.MultipleChoiceField(required=False)
class Meta:
model = Company
and I want to get the list of all selected emails in the view, something like this:
form = ContactsForm(request.POST)
if form.is_valid():
form.cleaned_data['emails'][0] # produces 1 and not email
There is no get_emails_display() kind of method, like in the model for example. Also, a suggestion form.fields['emails'].choices does not work, as it gives ALL the choices, whereas I need something like form.fields['emails'].selected_choices ?
Any ideas, or let me know if it's unclear.