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 the entire 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['my_dropdown'].choices = [x.id, x.name for x in YourMode.objects.all()]


Shawn



--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to