Creating forms from models
We need to build a form to let users comment on blog posts. Remember that Django has two base classes that can be used to create forms: Form
and ModelForm
. We used the Form
class to allow users to share posts by email. Now we will use ModelForm
to take advantage of the existing Comment
model and build a form dynamically for it.
Edit the forms.py
file of your blog
application and add the following lines:
from .models import Comment
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ['name', 'email', 'body']
To create a form from a model, we just indicate which model to build the form for in the Meta
class of the form. Django will introspect the model and build the corresponding form dynamically.
Each model field type has a corresponding default form field type. The attributes of model fields are taken into account for form validation. By default, Django creates a form field for each field...