Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Jan. 7, 2011 16:33:53

h.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How do I Bind a Form choice Field?


I want to code a form which will have a choice field which I want to
have rendered as a dropdown box in my html file. How do I code the
choices parameter in the form definition to bind it to a column in one
of my data tables in my model? The documentation shows a few examples,
but nothing specifically for choice fields. Please explain. Thanks.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Jan. 7, 2011 16:48:02

Shawn M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How do I Bind a Form choice Field?


On 01/07/2011 11:33 AM, hank23 wrote:I want to code a form which will have a choice field which I want to
have rendered as a dropdown box in my html file. How do I code the
choices parameter in the form definition to bind it to a column in one
of my data tables in my model? The documentation shows a few examples,
but nothing specifically for choice fields. Please explain. Thanks.You have to use a ModelForm for this.

class SomeForm(forms.ModelForm):

class Meta:
model = YourModel #Done!Technically you can do it with a forms.Form, but you have to do theentire thing manually, which is a big waste.class ManualFormNotWorthTheTrouble(forms.Form):

my_dropdown = forms.ChoiceField()

def __init__(self, *args, **kwargs):

#do normal __init__
super(ManualFormNotWorthTheTrouble, self).__init__(*args, **kwargs)self.fields.choices = Shawn



--
You received this message because you are subscribed to the Google Groups "Django
users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Jan. 7, 2011 17:02:10

Daniel R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How do I Bind a Form choice Field?


On Friday, January 7, 2011 4:47:43 PM UTC, ShawnMilo wrote:
>
> On 01/07/2011 11:33 AM, hank23 wrote:
> > I want to code a form which will have a choice field which I want to
> > have rendered as a dropdown box in my html file. How do I code the
> > choices parameter in the form definition to bind it to a column in one
> > of my data tables in my model? The documentation shows a few examples,
> > but nothing specifically for choice fields. Please explain. Thanks.
> >
>
> You have to use a ModelForm for this.
>
> <snip>
>
> Technically you can do it with a forms.Form, but you have to do the
> entire thing manually, which is a big waste.
>
> <snip>
>
> Shawn
>

Er, no, both wrong. The source of choices for a field has nothing to do with
whether or not it's a modelform, and there is an easy built-in field class
for managing exactly what the OP requested: ModelChoiceField. This takes a
`queryset` parameter, which provides the data for the field choices. See the
docs:http://docs.djangoproject.com/en/1.2/ref/forms/fields/#modelchoicefield--
DR.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Jan. 7, 2011 19:03:47

h.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How do I Bind a Form choice Field?


So I can create a queryset and pass its variable in the choices
parameter to bind a modelchoicefield from a modelform to a particular
iterable object? So then does this work only with modelforms or with
any kind of form (one coded manually as well)? If I can do this with
any form then where do I code the choices parameter? The only examples
I've seen show it being coded using dictionaries containing hard-coded
values and being coded as part of the form in question. Let me know
as soon as you can. Thanks for the help.

On Jan 7, 11:02 am, Daniel Roseman <dan...@roseman.org.uk> wrote:
> On Friday, January 7, 2011 4:47:43 PM UTC, ShawnMilo wrote:
>
> > On 01/07/2011 11:33 AM, hank23 wrote:
> > > I want to code a form which will have a choice field which I want to
> > > have rendered as a dropdown box in my html file. How do I code the
> > > choices parameter in the form definition to bind it to a column in one
> > > of my data tables in my model? The documentation shows a few examples,
> > > but nothing specifically for choice fields. Please explain. Thanks.
>
> > You have to use a ModelForm for this.
>
> > <snip>
>
> > Technically you can do it with a forms.Form, but you have to do the
> > entire thing manually, which is a big waste.
>
> > <snip>
>
> > Shawn
>
> Er, no, both wrong. The source of choices for a field has nothing to do with
> whether or not it's a modelform, and there is an easy built-in field class
> for managing exactly what the OP requested: ModelChoiceField. This takes a
> `queryset` parameter, which provides the data for the field choices. See the
> docs:http://docs.djangoproject.com/en/1.2/ref/forms/fields/#modelchoicefield> --
> DR.- Hide quoted text -
>
> - Show quoted text -

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 Jan. 7, 2011 19:26:51

h.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How do I Bind a Form choice Field?


So when I would code the queryset parameter in the form definition I
would just code something like this:

pollquestions =
forms.ModelChopiceField(queryset=Poll.objects.all(), ...)

when I define the pollquestions field of my form which is ultimately
to be rendered into the dropdown box on my form? And then also follow
that parameter with any other optional ones, like widget, etc.?

On Jan 7, 1:03 pm, hank23 <hversem...@stchas.edu> wrote:
> So I can create a queryset and pass its variable in the choices
> parameter to bind a modelchoicefield from a modelform to a particular
> iterable object? So then does this work only with modelforms or with
> any kind of form (one coded manually as well)? If I can do this with
> any form then where do I code the choices parameter? The only examples
> I've seen show it being coded using dictionaries containing hard-coded
> values and being coded as part of the form in question.  Let me know
> as soon as you can. Thanks for the help.
>
> On Jan 7, 11:02 am, Daniel Roseman <dan...@roseman.org.uk> wrote:
>
>
>
> > On Friday, January 7, 2011 4:47:43 PM UTC, ShawnMilo wrote:
>
> > > On 01/07/2011 11:33 AM, hank23 wrote:
> > > > I want to code a form which will have a choice field which I want to
> > > > have rendered as a dropdown box in my html file. How do I code the
> > > > choices parameter in the form definition to bind it to a column in one
> > > > of my data tables in my model? The documentation shows a few examples,
> > > > but nothing specifically for choice fields. Please explain. Thanks.
>
> > > You have to use a ModelForm for this.
>
> > > <snip>
>
> > > Technically you can do it with a forms.Form, but you have to do the
> > > entire thing manually, which is a big waste.
>
> > > <snip>
>
> > > Shawn
>
> > Er, no, both wrong. The source of choices for a field has nothing to do with
> > whether or not it's a modelform, and there is an easy built-in field class
> > for managing exactly what the OP requested: ModelChoiceField. This takes a
> > `queryset` parameter, which provides the data for the field choices. See the
> > docs:http://docs.djangoproject.com/en/1.2/ref/forms/fields/#modelchoicefield> > --
> > DR.- Hide quoted text -
>
> > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#6 Jan. 7, 2011 21:01:28

Mike S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How do I Bind a Form choice Field?


Hank -

Here's the answer you are looking for:http://docs.djangoproject.com/en/dev/ref/forms/fields/#modelchoicefieldHere's an example:

stuff = forms.ModelChoiceField(queryset=Series.objects.values('whatever'),
empty_label=" ")

Spend some serious time learning modelforms. Most of the time, they are the
right answer. Modelforms are different than regular forms... so you do have to
populate the choices differently. The key with Django is to understand when
you need a modelform, and when you need a regular form.

modelform - Django builds forms from models.
form - You design what you want, but have to do a lot more work to populate
the form with data.



There you go.

On Friday, January 07, 2011 02:03:39 pm hank23 wrote:
> So I can create a queryset and pass its variable in the choices
> parameter to bind a modelchoicefield from a modelform to a particular
> iterable object? So then does this work only with modelforms or with
> any kind of form (one coded manually as well)? If I can do this with
> any form then where do I code the choices parameter? The only examples
> I've seen show it being coded using dictionaries containing hard-coded
> values and being coded as part of the form in question. Let me know
> as soon as you can. Thanks for the help.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#7 Jan. 7, 2011 21:24:27

h.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How do I Bind a Form choice Field?


Thanks for the help.

On Jan 7, 3:01 pm, Mike Seidle <m...@seidle.net> wrote:
> Hank -
>
> Here's the answer you are looking for:
>
>http://docs.djangoproject.com/en/dev/ref/forms/fields/#modelchoicefield>
> Here's an example:
>
> stuff = forms.ModelChoiceField(queryset=Series.objects.values('whatever'),  
> empty_label="     ")
>
> Spend some serious time learning modelforms. Most of the time, they are the
> right answer.  Modelforms are different than regular forms... so you do have
> to
> populate the choices differently.  The key with Django is to understand when
> you need a modelform, and when you need a regular form.
>
> modelform - Django builds forms from models.
> form - You design what you want, but have to do a lot more work to populate
> the form with data.
>
> There you go.
>
> On Friday, January 07, 2011 02:03:39 pm hank23 wrote:
>
>
>
> > So I can create a queryset and pass its variable in the choices
> > parameter to bind a modelchoicefield from a modelform to a particular
> > iterable object? So then does this work only with modelforms or with
> > any kind of form (one coded manually as well)? If I can do this with
> > any form then where do I code the choices parameter? The only examples
> > I've seen show it being coded using dictionaries containing hard-coded
> > values and being coded as part of the form in question.  Let me know
> > as soon as you can. Thanks for the help.- Hide quoted text -
>
> - Show quoted text -

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 14th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support