Following on from this example, I am trying to use values from a table to populate a choicefield (and been at it for 2 days trying every method I can find in google....)
Clearly from the output below, I am creating the wrong kind of data type, but don't know how to change it: THIS VERSION WORKS: >>> CHOICES = ( ('P', 'Pending'), ('A', 'Active'), ('D', 'Done')) >>> print CHOICES (('P', 'Pending'), ('A', 'Active'), ('D', 'Done')) >>> fuel_type = forms.ChoiceField(choices=CHOICES,widget=forms.Select ()) >>> print fuel_type.choices [('P', 'Pending'), ('A', 'Active'), ('D', 'Done')] >>> THIS VERSION DOES NOT: (fails with Caught an exception while rendering: need more than 1 value to unpack) >>> CHOICES = Car.objects.all().values('fuel_type','fuel_type').distinct() >>> print CHOICES [{'fuel_type': u'Petrol'}, {'fuel_type': u'Diesel'}] >>> fuel_type = forms.ChoiceField(choices=CHOICES,widget=forms.Select ()) >>> print fuel_type.choices [{'fuel_type': u'Petrol'}, {'fuel_type': u'Diesel'}] Any help as to the best way of doing this would be great - I also need to add 'Any Fuel' as the first option as this is for a search form. On Dec 29 2008, 11:10 am, Polat Tuzla <ptu...@gmail.com> wrote: > A formatting error occurred while I did my previous post. > Please make sure that you notice the parentheses at the last line of > the code snippet, which were actually meant to be at the end of the > previous line. > > Regards, > Polat > > On Dec 29, 1:03 pm, Polat Tuzla <ptu...@gmail.com> wrote: > > > You can do it as follows: > > > class MyForm: > > CHOICES = ( ('P', 'Pending'), ('A', 'Active'), ('D', 'Done')) > > status = forms.ChoiceField(choices=CHOICES,widget=forms.Select > > ()) > > > Regards, > > Polat > > > On Dec 29, 5:13 am, "Aaron Lee" <waifun...@gmail.com> wrote: > > > > I would like to populate aformfield which uses aSelectwidget with > > > choices in views.py. > > > I understand I can pass the initial arg to theformbut I couldn't find the > > > correct value to pass. > > > > The choices is a list of tuple > > > CHOICES = ( ('P', 'Pending'), ('A', 'Active'), ('D', 'Done')) > > > > Any hints? > > > > -Aaron > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---