Django: Getting Started Transcripts
Chapter: Posting Data
Lecture: Building a 'ModelForm' based on the 'ReviewModel'

Login or purchase this course to watch this video and the rest of the course contents.
0:00 The concept of a form in Django is very similar to the concept of a model An object defines some fields.
0:08 You can write form objects to describe any form you like. But there is a shortcut model forms. Model forms are forms based on models,
0:16 you can specify the model object and Django figures out the rest. You'll be importing these forms so it doesn't matter where they go.
0:24 But the convention is to call it forms.py. Let me create one of those inside of review.
0:42 To get started you need to import the model form object from the Django forms module and as I'm going to build a form around the review model,
0:53 I have to import that as well. The review form object has an inner meta class with some configuration.
1:04 The model attribute says what model to base this form upon. If you want all the fields from your model, you're done, that's it, model equals review.
1:14 In my case, I only want the rating and text fields so I can restrict it by specifying the fields attribute.
1:22 Using this object, Django constructs a form using the information from the review model.
1:28 Remember all that validation stuff built in because the rating field is restricted to a number
1:33 between one and five, the form will validate that as well. This is why you use a model form. It's far less work.


Talk Python's Mastodon Michael Kennedy's Mastodon