How to Create a ModelForm in Django



Python


In this article, we show how to create a ModelForm in Django.

So what is a ModelForm in Django?

A ModelForm in Django is just like a regular form, except it is created based off of an existing database table on the website.

A ModelForm would be created, for example, if you have a BlogComment form that goes into a BlogComment database table.

This allows for synchronchy of form data with database data, and they could be perfectly matched.

So, a ModelForm is just a form that takes its fields from an existing database table.

Therefore, we don't have to recreate all the fields and everything, which would be redundant.

So let's say we have a form called BlogComments, shown below.



So you can see that we have a database table called BlogComments. You know that this is a database table because it is specified, models.Model

This database table has 4 fields, firstname, lastname, email and comment.

So this is the database table that we have.

So now let's create the ModelForm based off of this database table.


Creating the ModelForm

So now we go to the forms.py file and create the ModelForm.

The code to do this is shown below.



So let's now explain this code we have above.

First we import forms from django because we're going to create a form based on the database table we created.

We also must import the database table we created in the models.py file.

We then create our form, which we'll call BlogCommentsForm.

Underneath this we have class Meta. What class meta is data that allows us to explicitly tell Django which fields of the database table to include in the form. Two variables that may be included in this class Meta are fields and exclude.

Here we just have included Fields. In this fields list, we specify all the fields that we want to include in the form from the database table. In this example, I include all the fields from the Django database table. However, you can don't have to include all fields from the database if you don't want to. In the include list, you simply list those fields that you want to include.

An alternative of going about this is if you don't want to list all the fields and you want to include all of them, don't put an include list and instead simply make an exclude list and set it equal to an empty list. Therefore, you would have, exclude=[]. Since nothing is included in exclude[], Django knows to include all fields.

Another alternative is if you want to include some fields and exclude others is to have the fields you want to include in the include list and have those fields you want to exclude in the exclude list. Even though this is not necessary, it's more explicit and makes it directly clear which fields you want to include and which you want to exclude.

Either way, with class meta, you must have at least one list, include or exclude, present, or else Django will throw an error.

So now that we've created our form field from the database table, we just now have to go to the views.py file and pass in the form to view, so that users can go in and enter data into the form.


views.py File

So now that we've created our form from the database table in the models.py file, the last thing to do is just to render the file into view and also save the data to our database.

This is what we do in the code below.



So in our views.py file, we import render to render our HTML template file.

Then we import the form that we created in our forms.py file, based off of our database table. The form is named BlogCommentsForm.

We then create a function called showform().

We create a variable called form and set it equal to BlogCommentsForm(request.POST or None). The reason we set it equal to request.POST or None is so that it retains the data that a user enters into the form after the submit button is pressed. or None is so that it doesn't raise validation errors before a user has pressed the submit button.

We then write that if the form is valid, we save the form using the statement, form.save(). This statement saves the form data to the database.

We then pass in the form into the context dictionary and this context dictionary gets renders into the HTML template file.

And this is all that is needed to create a ModelForm in Django.

Now any time you go to this form and enter in data and click 'Submit', the data gets saved right to the database.

So Django is excellent with having form data be entered directly into a database and saved.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...