For some applications, you may want to create your own fancy forms, but sometimes the basic model form is almost, but not quite what you want; in that case, you can directly modify the fields at run-time. A ModelForm has a field called fields, which is a dictionary, indexed by field names.
One situation I faced recently is that I wanted to limit the choices on a choice field; choice fields in django have a field called queryset, which you can replace with any queryset you want; I needed to limit the programs a user could access, based on which programs it was registered for; all I had to do was
form.fields['program'].queryset=models.Program.objects.filter(userprogram__user=u)
No comments:
Post a Comment