How do I construct a Django form with model objects in a Select widget?
Posted
by Thierry Lam
on Stack Overflow
See other posts from Stack Overflow
or by Thierry Lam
Published on 2010-03-31T19:54:52Z
Indexed on
2010/03/31
20:03 UTC
Read the original article
Hit count: 220
django
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?
© Stack Overflow or respective owner