How do I construct a Django form with model objects in a Select widget?
- by Thierry Lam
Let's say I'm using the Django Site model:
class Site(models.Model):
name = models.CharField(max_length=50)
My Site values are (key, value):
1. Stackoverflow
2. Serverfault
3. Superuser
I want to construct a form with an html select widget with the above values:
<select>
<option value="1">Stackoverflow</option>
<option value="2">Serverfault</option>
<option value="3">Superuser</option>
</select>
I'm thinking of starting with the following code but it's incomplete:
class SiteForm(forms.Form):
site = forms.IntegerField(widget=forms.Select())
Any ideas how I can achieve that with Django form?