Blog

Django ModelForm | Complete Django Tutorials

Django ModelForm copy
Django Django Tutorials

Django ModelForm | Complete Django Tutorials

Complete Tutorials on Django ModelForm

We will learn about Django ModelForm in the Django tutorial. But before we have to understand what is basically a Model is? Model is the source of information about the entity. When we create an application we need some data that is used entire application. That data needs to store in the database. To store the database we have to create tables. The SQL language is used to create a database table. So in Django you did not need to learn SQL, you have to learn how to create a model in Django then your database table will be created and you will be able to store data in a database table.

What is Django ModelForm

ModelForm is a class that is taken from forms (forms.ModelForm), you need to use while creating a model form for your Django model. When we create a ModelForm for our model then Django directly converts a model into Django form. That form is used to collect data from users, then such data will be store in that database model form directly. It is a very easy and beneficial way to create form and storing data in the table. The form is created in the Django ModelForm class, it will map to that specific model for which form is created.

Creating a Django Model

To create a Model class, open the models.py file in your application folder. Then import models from django.db then define your model class for any entity which has some properties or attributes. You will define the model’s fields according to their attributes.

from django.db import models
class All_user(models.Model):
    first_name = models.CharField(max_length=30) 
    last_name = models.CharField(max_length=30)
The above line of code is telling, there is model name All_user which is inheriting class Model. It has two fields first_name and last_name that are related to text-based fields.
So, we make run a command to convert into SQL query then we use the following command:
python manage.py makemigrations
Then you have to run the following command:
python manage.py migrate

Creating its Django ModelForm

When you run migrate command, it will create a table in your database. In that table, there will be two fields as you mentioned in model class fields. So, now you have to create a ModelForm in order to create a form, where users input data, and you will get that data in that table as you created right now.
from django.forms import ModelForm
from myapp.models import Article
# Create the form class.  
class All_userForm(ModelForm):
    class Meta:
        model = All_user
        fields = [‘first_name ‘, ‘last_name ‘]

Render ModelForm in views

In the following code, you can understand how you can render a ModelForm in veiw section, First of all you have to import your model form from forms.py file. Then you can use model form in order to dispaly on browser to get data from users.

from .forms import All_userForm

def settings(request):
    if request.method == “POST”:
        userForm= All_userForm(request.POST)
        if userForm.is_valid():
            userForm.save()
    else:
        userForm= All_userForm()
    context = {‘userForm’:userForm}
    return render(request, ‘users/profile.html’, context)

Render a ModelForm in the template

Now, you have to render a form in a template. As you passed a dictionary key in the view section userForm. Then use userForm as a variable in the form tag. If you are using the post method then you should use the CSRF token to avoid CSRF attack on your request.


<
form action=“” method=“post”>

{% csrf_token %}

{{ userForm}}

<input type=“submit” value=“Submit”>

</form>

When you run a server, you will see on the browser a form in which the user will input data. And data will store in a database table that is developed when you create a model class.

Conclusion

Django ModelForm is the best option to create a database table and  Django forms that map to each other. Rapidly way in development that is provided by Django. When you create a Django website or web application, you should use Model Form to achieve requirements.

If you have any questions in your mind while reading out the tutorial, then must share them with us. We will provide you the best solution possible.

If you use Facebook, Twitter, Pinterest, LinkedIn, or other social media platforms, then share this Django tutorial, if you share, more students will take the chance to learn.

Recommended Django Tutorial for Beginners

How to Retrieve DjangoForm Data Complete Guide | Django Tutorials

Render Form Field Manually – Django Form | Django Tutorials

Django Form Validation | Complete Django Tutorials

Hack – Django User Authentication System | Django Tutorials

 

Leave your thought here

Your email address will not be published.

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
  • Attributes
  • Custom attributes
  • Custom fields
Click outside to hide the compare bar
Compare
Wishlist 0
Open wishlist page Continue shopping