I’m struggling to get my head round django forms.. I’ve been reading various documentation but just can’t quite grasp the concepts. I have got to grips with models, views and templates. What I am trying to do is to create a form with various fields composing of dropdown lists and checkboxes which are populated by values in a database.
I have a working app called vms. Using the models.py I have a built a simple schema that holds size and type. Size consists of ‘small’, ‘medium’ & ‘large’. Type is ‘windows’ & ‘linux’. Using the admin site, I can add an extra size, for example ‘Extra Large’.
What I would like to do is create a form that has a drop down list of the vm sizes. If an extra size gets added via the admin site, I would like that size to appear in the drop down list.
I would submit my attempts at the code, but actually am struggling with the concepts. Can anyone help guide me in how to accomplish the above?
Thanks
Oli
Forms are just a tool to simplify and speed-up (the development of) the process of fetching POST data from the request. A manual way would be to do
request.POST.get('somefield')for all the fields there are in some HTML form. But Django can do better than that…In its essence, a Form class holds a number of Fields and performs these tasks:
form.cleaned_datadictionary as a convenient way to access these values in view.With these values, I could then manually create a new instance of a
MyModeland save it. Of course, I would have to define a Field in the Form for every Field in MyModel model.This means that, basically, I could do something like this:
(forgive me for not testing this code, so I can’t vouch that it’s 100% correct)
(this could be written with a few lines of code less, but it’s meant to be as clear as possible)
Notice there are no relation between model Fields and form Fields! We have to manually assign values to MyModel instance when creating it.
The above example outlines generic form workflow. It is often needed in complex situations, but not in such a simple one as is this example.
For this example (and a LOT of real-world examples), Django can do better than that…
You can notice two annoying issues in the above example:
MyModeland Fields onFormForMyModelseparately. However, there is a lot of similarity between those two groups (types) of Fields, so that’s kind of duplicate work. The similarity grows when adding labels, validators, etc.MyModelinstance is a bit silly, having to assign all those values manually.This is where a ModelForm comes in.
These act basically just like a regular form (actually, they are extended from regular forms), but they can save me some of the work (the two issues I just outlined, of course 🙂 ).
So back to the two issues:
Instead of defining a form Field for each model Field, I simply define
model = MyModelin the theMetaclass. This instructs the Form to automatically generate form Fields from model Fields.Model forms have
savemethod available. This can be used to create instance of model in one line in the view, instead of manually assigning field-by-field.So, lets make the example above with a
ModelForm:Hope this clears up the usage of Django forms a bit.
Just one more note – it is perfectly ok to define form Fields on a
ModelForm. These will not be used inform.save()but can still be access withform.cleaned_datajust as in a regular Form.