[Answered ]-List Box Django Forms

2👍

Your NO_OF_HRS tuple is defined inside the model and not available to the form. It has to be imported in forms.py just like any other Python object. Try moving the tuple outside the model definition and import in your forms.py like this:

models.py

NO_OF_HRS = (('1','1')
             ('2','2'))

class Volunteer(models.Model):
    # ...
    duration = models.CharField(choices=NO_OF_HRS, max_length=1)

forms.py

from path.to.models import NO_OF_HRS

class VolunteerForm(forms.Form):
    # ...
    duration = forms.CharField(widget=forms.Select(choices=NO_OF_HRS), max_length=1)

It also looks like you want to use a ModelForm. In this case you don’t need to add any field definitions to your VolunteerForm, simply set your model in the inner Meta class.

forms.py

from path.to.models Volunteer

class VolunteerForm(forms.ModelForm):
    class Meta:
        model = Volunteer

Leave a comment