Reputation: 21
I am learning Django forms. When should I use ModelForms
rather than Forms
? Which is more suitable for production?
Upvotes: 1
Views: 710
Reputation: 336
I am kinda lost, so, why should I used modelforms instead of using just forms
Conceptually, ModelForm
-derived form classes aim to replicate the underlying model's behavior as closely as possible. I use 'behavior' here in a general sense to mean: a) model fields (i.e., data); and b) model constraints (i.e., everything else, which might include model field validators or custom business logic or anything else).
From an implementation standpoint, ModelForm
has a similar initial basis to Form
.
However unlike Form
, ModelForm
allows tight Model
instance integration.
ModelForm
also allows:
Form
rather than ModelForm
, you'd need to re-declare any model fields in your Form
class. This would mean that you would duplicate application logic. To illustrate this point, see the below from the official Django ModelForm
docs):### models.py from django.db import models class Book(models.Model): """ Sample book model. """ name = models.CharField(max_length=100) authors = models.ManyToManyField(Author) ### forms.py from django import forms class BookForm(forms.Form): """ Your `forms.Form` subclass loosely representing the `Book` model. """ name = forms.CharField(max_length=100) authors = forms.ModelMultipleChoiceField(queryset=Author.objects.all()) # Alternatively, class BookForm(forms.ModelForm): """ Your `forms.ModelForm` subclass representing the `Book` model directly. """ class Meta: model = Book fields = ['name', 'authors']
model fields to be included or explicitly excluded from the resulting form
model formsets to be created just as easily.
which one is more suitable for production
Both kinds of forms may fit most setups. Both ModelForm
and Form
are native Django web framework components. Your choice of whether to use ModelForm
or Form
will depend on the specific project's requirements, as well as personal preferences and adopted code conventions.
In summary, if a model can be represented easily by using a ModelForm
instance, use ModelForm
. If not, use a customized Form
.
Upvotes: 3
Reputation: 2439
If you have in your application a model for example Book with a field title
models.py
Class Book(models.Model):
title = models.CharField()
To represent it in a form you have two options
forms.py
from myapp.models import Book
class BookSimpleForm(forms.Form):
title = forms.CharField()
or
class BookModelForm(forms.ModelForm):
class Meta:
model = Book
fields = ('title',)
When you use to save the data of the form in the simple Form you have to write the code of the save method because Form isn't attached to a model. When you use a modelForm because in the class Meta you specific tell the form when it runs the save method , save the posted data to the model of the instance of the modelform.
in views.py
from app.models import Book
from app.forms import BookSimpleForm, BookModelForm
First option
def method(request):
if request.method == 'POST':
book = Book(request.POST)
book.save()
Second option
def method(request):
if request.method == 'POST':
form = BookModelForm(request.POST)
if form.is_valid():
form.save()
In the first method request.Post has the posted data (title) so you create an instance of a book and save it . ModelForm has method save so as to save the posted data via the modeform save method and to validate them
Upvotes: 2