Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django's forms.Form vs forms.ModelForm

Could anyone explain to me similarities and differences of Django's forms.Form & forms.ModelForm?

like image 835
Viet Avatar asked Feb 20 '10 18:02

Viet


People also ask

What is forms ModelForm in django?

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 and form fields.

Should I use django form or HTML form?

If you are using django to develop your website, I think it is best to only use django-forms since they have built in validation and can easily be linked with your models. You also will have consistent formatting and don't need to type out the html every time.

How many types of django forms are there?

Django form fields define two types of functionality, a form field's HTML markup and its server-side validation facilities.

What is model form?

Model Forms are forms that are connected directly to models, allowing them to populate the form with data. It allows you to create a form from a pre-existing model. You add an inline class called Meta, which provides information connecting the model to the form. An inline class is a class within another class.


2 Answers

Forms created from forms.Form are manually configured by you. You're better off using these for forms that do not directly interact with models. For example a contact form, or a newsletter subscription form, where you might not necessarily be interacting with the database.

Where as a form created from forms.ModelForm will be automatically created and then can later be tweaked by you. The best examples really are from the superb documentation provided on the Django website.

forms.Form:
Documentation: Form objects
Example of a normal form created with forms.Form:

from django import forms  class ContactForm(forms.Form):     subject = forms.CharField(max_length=100)     message = forms.CharField()     sender = forms.EmailField()     cc_myself = forms.BooleanField(required=False) 

forms.ModelForm:
Documentation: Creating forms from models
Straight from the docs:

If your form is going to be used to directly add or edit a Django model, you can use a ModelForm to avoid duplicating your model description.

Example of a model form created with forms.Modelform:

from django.forms import ModelForm from . import models  # Create the form class. class ArticleForm(ModelForm):     class Meta:         model = models.Article 

This form automatically has all the same field types as the Article model it was created from.

like image 143
jathanism Avatar answered Sep 24 '22 01:09

jathanism


The similarities are that they both generate sets of form inputs using widgets, and both validate data sent by the browser. The differences are that ModelForm gets its field definition from a specified model class, and also has methods that deal with saving of the underlying model to the database.

like image 29
Ignacio Vazquez-Abrams Avatar answered Sep 23 '22 01:09

Ignacio Vazquez-Abrams