Django ModelForm - Create form from Models
Last Updated :
19 May, 2025
ModelForm is a class that automatically generates a "form" from a Django model. It reduces boilerplate code by linking your form fields directly to your model fields, making form creation faster, cleaner, and less error-prone. It also provides built-in methods and validation to streamline form processing.
To explain the working of the project, we will use project geeksforgeeks, create a model and map it to Django forms.
Refer to the following articles to check how to create a project and an app in Django.
Step 1: Create the Model
In geeks/models.py, define a model as follows:
Python
from django.db import models
class GeeksModel(models.Model):
title = models.CharField(max_length = 200)
description = models.TextField()
last_modified = models.DateTimeField(auto_now_add = True)
img = models.ImageField(upload_to = "images/")
def __str__(self):
return self.title
This model has a title, description, timestamp, and an image.
Step 2: Register Your App
Before creating migrations, ensure the geeks app is added to the INSTALLED_APPS list in your project's settings.py:
INSTALLED_APPS = [
# other apps,
'geeks',
]
Step 3: Make Migrations and Migrate
Run the following commands to create and apply the migration for your model:
Python manage.py makemigrations
Python manage.py migrate
Step 4: Verify Model Creation
You can verify that the model is registered by visiting the Django admin interface:
http://127.0.0.1:8000/admin/geeks/geeksmodel/add/
In geeks/forms.py, create a ModelForm class to automatically generate a form based on your model:
Python
from django import forms
from .models import GeeksModel
class GeeksForm(forms.ModelForm):
class Meta:
model = GeeksModel
fields = "__all__"
This form takes two arguments fields or exclude.
fields vs exclude
- fields: A list or special value '__all__' indicating which model fields should be included in the form. It is highly recommended to explicitly specify the fields to avoid potential security risks.
- exclude: A list of fields to exclude from the form. For example:
Python
class PartialGeeksForm(forms.ModelForm):
class Meta:
model = GeeksModel
exclude = ['img']
Update your view to instantiate and process the form data:
Python
from django.shortcuts import render
from .forms import GeeksForm
def home_view(request):
context ={}
form = GeeksForm(request.POST or None, request.FILES or None)
if form.is_valid():
form.save()
context['form']= form
return render(request, "home.html", context)

Everything is set, Now visit "http://127.0.0.1:8000/",  Now you can see that every model field is mapped into a form field and displayed correspondingly. Field mappings are discussed later in this article. So now let's try entering data into the form and check if it gets saved into the database.Â

Hit submit and Bingo the form gets saved automatically to database. We can verify it at:
http://localhost:8000/admin/geeks/geeksmodel/

Field types
The generated Form class will have a form field for every model field specified, in the order specified in the fields attribute. Each model field has a corresponding default form field. For example, a CharField on a model is represented as a CharField on a form. A model ManyToManyField is represented as a MultipleChoiceField. Here is the full list of conversions:
Model Field | Form Field |
---|
AutoField | Not represented in the form |
---|
BigAutoField | Not represented in the form |
---|
BigIntegerField | IntegerField with min_value set to -9223372036854775808 and max_value set to 9223372036854775807. |
---|
BinaryField | CharField, if editable is set to True on the model field, otherwise not represented in the form. |
---|
BooleanField | BooleanField, or NullBooleanField if null=True. |
---|
CharField | CharField with max_length set to the model field’s max_length and empty_value set to None if null=True. |
---|
DateField | DateField |
---|
DateTimeField | DateTimeField |
---|
DecimalField | DecimalField |
---|
DurationField | DurationField |
---|
EmailField | EmailField |
---|
FileField | FileField |
---|
FilePathField | FilePathField |
---|
FloatField | FloatField |
---|
ForeignKey | ModelChoiceField |
---|
ImageField | ImageField |
---|
IntegerField | IntegerField |
---|
IPAddressField | IPAddressField |
---|
GenericIPAddressField | GenericIPAddressField |
---|
ManyToManyField | ModelMultipleChoiceField |
---|
NullBooleanField | NullBooleanField |
---|
PositiveIntegerField | IntegerField |
---|
PositiveSmallIntegerField | IntegerField |
---|
SlugField | SlugField |
---|
SmallAutoField | Not represented in the form |
---|
TextField | CharField with widget=forms.Textarea |
---|
TimeField | TimeField |
---|
URLField | URLField |
---|
Similar Reads
Django Form
When one creates a Form class, the most important part is defining the fields of the form. Each field has custom validation logic, along with a few other hooks. This article revolves around various fields one can use in a form along with various features and techniques concerned with Django Forms. D
6 min read
How to create a form using Django Forms ?
Django forms are an advanced set of HTML forms that can be created using python and support all features of HTML forms in a pythonic way. This post revolves around how to create a basic form using various Form Fields and attributes. Creating a form in Django is completely similar to creating a model
3 min read
Django ModelFormSets
ModelFormsets in a Django is an advanced way of handling multiple forms created using a model and use them to create model instances. In other words, ModelFormsets are a group of forms in Django. One might want to initialize multiple forms on a single page all of which may involve multiple POST requ
4 min read
Django ModelForm
Django ModelForm is a class that is used to directly convert a model into a Django form. If youâre building a database-driven app, chances are youâll have forms that map closely to Django models. For example, a User Registration model and form would have the same quality and quantity of model fields
4 min read
Render Django Forms as table
Django forms are an advanced set of HTML forms that can be created using python and support all features of HTML forms in a pythonic way. Rendering Django Forms in the template may seem messy at times but with proper knowledge of Django Forms and attributes of fields, one can easily create excellent
2 min read
Render Django Forms as paragraph
Django forms are an advanced set of HTML forms that can be created using python and support all features of HTML forms in a pythonic way. Rendering Django Forms in the template may seem messy at times but with proper knowledge of Django Forms and attributes of fields, one can easily create excellent
2 min read
Render Django Forms as list
Django forms are an advanced set of HTML forms that can be created using python and support all features of HTML forms in a pythonic way. Rendering Django Forms in the template may seem messy at times but with proper knowledge of Django Forms and attributes of fields, one can easily create excellent
2 min read
Render HTML Forms (GET & POST)
Django is often called a "Batteries Included Framework" because it provides built-in settings and features that help developers build websites rapidly and efficiently. One of the essential components in web development is handling HTML forms, a way for users to send data to the server for processing
3 min read
Django form field custom widgets
A widget is Djangoâs representation of an HTML input element. The widget handles the rendering of the HTML, and the extraction of data from a GET/POST dictionary that corresponds to the widget. Whenever you specify a field on a form, Django will use a default widget that is appropriate to the type o
3 min read
Initial form data _ Django Forms
When using Django forms, we may want to automatically fill in some fields with default values. This is called initial data and it's different from placeholders because it actually fills the fields. When the form is submitted, this data is treated just like anything the user types in.Django offers mu
3 min read