Re: How to populate a form field with a Select widget

2009-01-07 Thread phoebebright

Ahh yes, much more elegant.

Thanks.

On Jan 7, 11:32 am, bruno desthuilliers
 wrote:
> On 7 jan, 11:38, phoebebright  wrote:
>
> > Finally got it working - and I'm sure there is a much clear way of
> > doing it...
>
> >     used_fuels= Car.objects.all().values('fuel_type').distinct()
> >     fuel_choices=[('Any','Any Fuel')]
> >     for f in used_fuels:
> >         for key,value in f.items():
> >                 fuel_choices.append((value,value))
> >     fuels = forms.ChoiceField(choices=fuel_choices)
>
> used_fuels = Car.objects.values_list('fuel_type', flat=True).distinct
> ()
> fuel_choices = [('Any', 'Any Fuel')] + [(item, item) for item in
> used_fuels]
> fuels = forms.ChoiceField(choices=fuel_choices)
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: How to populate a form field with a Select widget

2009-01-07 Thread bruno desthuilliers

On 7 jan, 11:38, phoebebright  wrote:
> Finally got it working - and I'm sure there is a much clear way of
> doing it...
>
> used_fuels= Car.objects.all().values('fuel_type').distinct()
> fuel_choices=[('Any','Any Fuel')]
> for f in used_fuels:
> for key,value in f.items():
> fuel_choices.append((value,value))
> fuels = forms.ChoiceField(choices=fuel_choices)



used_fuels = Car.objects.values_list('fuel_type', flat=True).distinct
()
fuel_choices = [('Any', 'Any Fuel')] + [(item, item) for item in
used_fuels]
fuels = forms.ChoiceField(choices=fuel_choices)



--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: How to populate a form field with a Select widget

2009-01-07 Thread phoebebright

Finally got it working - and I'm sure there is a much clear way of
doing it...


used_fuels= Car.objects.all().values('fuel_type').distinct()
fuel_choices=[('Any','Any Fuel')]
for f in used_fuels:
for key,value in f.items():
fuel_choices.append((value,value))
fuels = forms.ChoiceField(choices=fuel_choices)



On Jan 3, 9:38 pm, phoebebright  wrote:
> 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  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  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"  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
-~--~~~~--~~--~--~---



Re: How to populate a form field with a Select widget

2009-01-03 Thread phoebebright

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  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  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"  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
-~--~~~~--~~--~--~---



Re: How to populate a form field with a Select widget

2008-12-29 Thread Polat Tuzla

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  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"  wrote:
>
>
>
> > I would like to populate a form field which uses a Select widget with
> > choices in views.py.
> > I understand I can pass the initial arg to the form but 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
-~--~~~~--~~--~--~---



Re: How to populate a form field with a Select widget

2008-12-29 Thread Polat Tuzla

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"  wrote:
> I would like to populate a form field which uses a Select widget with
> choices in views.py.
> I understand I can pass the initial arg to the form but 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
-~--~~~~--~~--~--~---



How to populate a form field with a Select widget

2008-12-28 Thread Aaron Lee
I would like to populate a form field which uses a Select widget with
choices in views.py.
I understand I can pass the initial arg to the form but 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
-~--~~~~--~~--~--~---