Preliminary preparation of the form of Django (non-model)

I am trying to pre-populate data in a django form based on some information, but NOT using ModelForm, so I cannot just install the instance.

It seems like it should be very simple, but for some reason I cannot find the 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.

+44
django django-forms
Jun 01 '09 at 19:35
source share
4 answers

Try:

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

The constructor of Form objects can accept a dictionary of field values. This creates a linked form that can be used to validate the data and display the form as HTML with the data displayed. See the API Forms document for more details.

Edit:

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

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

For more information, see the initial values documentation.

+78
Jun 01 '09 at 19:42
source share

There are two ways to fill out a Django form.

The first is to pass the dictionary as the first argument when creating it (or pass it as data kwarg, which is one and the same). This is what you do when you want to use the POST data to fill out and validate the form.

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

However, this will lead to a validation of the form, so it only works if you really provide valid and complete data to begin with, otherwise you will start with errors.

Another way to fill out a form is to use the initial parameter (documented here ). This gives initial values ​​for the form fields, but does not cause validation. Therefore, it is suitable if you do not fill in all the values, for example.

 form = MyForm(initial=data_dict) 

To populate the selection box with initial , use the pk value.

+23
Jun 01 '09 at 20:31
source share

You can use model_to_dict () to convert an instance to a dictionary, and then fill out the form with this. Something like this should work:

 from django.forms.models import model_to_dict ... my_obj = MyModel.objects.get(abc=123) form = MyForm(initial=model_to_dict(my_obj)) 

Note. I am using django version 1.3

+11
Feb 20 '13 at 1:52
source share

For what it's worth, the way to view a FormView for this would be to override the FormView get_initial function. get_initial returns the original keyword arguments used by get_form_kwargs to create the form.

Docs:

  • for get_initial , here ,
  • for get_form_kwargs , here .

Code example:

 from django.views.generic.edit import FormView class MyFormView(FormView): def get_initial(self): initial = super(MyFormView, self).get_initial() # update initial field defaults with custom set default values: initial.update({'charfield1': 'foo', 'charfield2': 'bar'}) return initial 
0
Aug 27 '17 at 14:26
source share



All Articles