- February 26, 2007
- Pre .96
- newforms models
- 13 (after 13 ratings)
This is an excerpt from the form code used on this site; the tricky bit here is making the
choices for the
language field get filled in dynamically from
Language.objects.all() on each form instantiation, so that new languages can be picked up automatically. It also adds a blank choice at the beginning so that users can't accidentally ignore the field and incorrectly end up with whatever Language was first in the list.
If you use this, always remember that you have to call the superclass
__init__ before you set your dynamic choices, and that you need to accept
**kwargs so you can pass them to it.
ModelChoiceField will solve this, but it still seems to be a bit buggy.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
More like this
- assign fields dynamically in newforms by rubic 8 years ago
- Reorder fields directly in the ModelForm by HM 6 years, 9 months ago
- A Lazy ChoiceField implementation by lsbardel 5 years, 4 months ago
- Enumeration field by nail.xx 6 years, 7 months ago
- Modify fields created by form_for_model by grahamu 8 years ago