Prepopulate Django (non-Model) Form

Viewed 47140

I'm trying to prepopulate the data in my django form based on some information, but NOT using ModelForm, so I can't just set the instance.

This seems like it should be really easy, but for some reason I can't find any documentation telling me how to do this. This is my form:

class MyForm(forms.Form):
  charfield1 = forms.CharField(max_length=3)
  charfield2 = forms.CharField(max_length=3)
  choicefield = forms.ModelChoiceField(MyModel.objects.all())

I tried just doing:

form = MyForm()
form.charfield1 = "foo"
form.charfield2 = "bar"
# a model choice field
form.choicefield = MyModel.objects.get(id=3)

which does not work.

4 Answers

Try:

form = MyForm({'charfield1': 'foo', 'charfield2': 'bar'})

The constructor of Form objects can take a dictionary of field values. This creates a bound form, which can be used to validate the data and render the form as HTML with the data displayed. See the forms API documentation for more details.

Edit:

For the sake of completeness, if you do not want to bind the form, and you just want to declare initial values for some fields, you can use the following instead:

form = MyForm(initial={'charfield1': 'foo', 'charfield2': 'bar'})

See the documentation of initial values for details.

There are two ways of populating a Django form.

The first is to pass a dictionary as the first argument when you instantiate it (or pass it as the data kwarg, which is the same thing). This is what you do when you want to use POST data to populate and validate the form.

data_dict = {'charfield1': 'data1', 'charfield2': 'data2', 'choicefield': 3}
form = MyForm(data_dict)

However, this will trigger validation on the form, so only works if you are actually passing in valid and complete data to begin with - otherwise you will start off with errors.

The other way to populate a form is to use the initial parameter (documented here). This gives initial values for the form fields, but does not trigger validation. It's therefore suitable if you're not filling in all values, for example.

form = MyForm(initial=data_dict)

To populate a choicefield via initial, use the pk value.

Related