What are Model Forms?6:30 with Kenneth Love
Django gives us a really handy way to quickly build forms based on our existing models.
Model forms are probably the part of the
forms library that you'll use more than any other.
One of the areas of model forms that frustrates people, but is amazingly important, is the requirement to use either
exclude. Many people find
excludes to be faster, simpler, easier because you don't have to update it every time you change your form or your model. But that's a dangerous decision to make because now any new fields will be automatically added to your displayed form. So use
fields instead! Be explicit and control your fields directly by using the
[MUSIC] 0:00 When you give an app a model, it's likely going to want a form for editing it. 0:04 Django's admin automatically creates forms for 0:08 creating and editing instances of our models once we register them. 0:11 But what if we wanna do it outside of the admin? 0:14 Django provides us with a special type of form known as a model form. 0:17 A model form is basically an automatically generated form 0:21 based on a particular model. 0:24 They provide a great easy way to create new model instances and 0:26 to edit existing ones. 0:29 This means we get all of the goodness of forms without having to write 0:31 an entire form ourselves. 0:34 So we have our models and we've already looked at doing standard forms, but 0:36 let's look at doing model forms. 0:40 Model forms are forms that are made off of our models. 0:42 So we want to do this basically the same way that we did our suggestion form. 0:46 So that means we want to come here and we want to add a new file named forms.py. 0:51 And in forms.py, we of course want to do from django import forms. 0:57 So far, so exactly the same as it was before. 1:03 All right, so now we need to bring in our models, so we'll say from. 1:07 import models and now let's go ahead and make our model forms. 1:11 Let's make a quiz form first, 1:18 this'll show pretty much all the stuff that we're going to do with a model form. 1:20 We'll say quiz form and this is forms.modelform, 1:25 thats the one difference right there, or,the first one rather, 1:30 is that we're going to do model form instead of just form. 1:33 And then we're not going to specify any fields because we're just going to use 1:37 the fields that the model gives us but we're gonna have to specify a class meta. 1:41 So in the class meta we have specify two things, 1:46 we have to specify the model which is gonna be models.quiz, 1:50 and then we have to specify fields and so what fields does, 1:56 there's two things here, and I'll talk about both of them. 2:01 So there's fields, which we specify the fields that we want to include. 2:07 So, for our quiz, we want to include title, description, order, and 2:13 total questions, cuz those are always there. 2:17 The opposite of this is exclude. 2:21 And if we were doing this with exclude instead of fields, 2:25 let's come over here and look at this quiz. 2:29 So we have title, description, order, course, and total questions. 2:32 So, we might just say okay, I'm going to exclude course. 2:40 Because course is the one field that I don't want to exclude. 2:44 This isn't a great idea, and 2:48 I'm gonna cover that in just a little bit on why that's not a great idea. 2:49 But just so you know that it's there. 2:53 What it does is it basically leaves off that field. 2:56 That sounds like a great idea, but there's a little gotcha. 2:59 We'll talk about that in a minute. 3:01 All right, so lets just do our fields and our model right there. 3:04 Lets go ahead and actually make a couple more classes. 3:11 Let's do a, I don't think we want to do a question form because we're 3:16 not going to answer, or we're not going to create questions directly, 3:20 we are going to create true false questions. 3:24 Or multiple choice questions. 3:26 So let's create a TrueFalseQuestion form. 3:27 And feel free to use shorter names on these if you want. 3:34 I tend to like really verbose names, as you've probably noticed. 3:38 So if you want something that's a little bit more terse, feel free to do that. 3:44 And we're going to do TrueFalseQuestion as our model. 3:49 And for fields we want to do order and 3:52 prompt, cuz those are the only questions we have for true false. 3:56 Or the only options we have for true false. 4:00 We might want to change our true false model. 4:03 I'm not going to do it in the course but you might want to change it so 4:05 that you can just mark on the question if it's true of if it's false. 4:09 Totally up to you. 4:14 And then for our last one we'll do multiple choice question form. 4:16 Oops, why did I put question form in here? 4:25 It's not a question form. 4:26 It's a model form. 4:27 Forms.ModelForm. 4:31 Just not thinking things through today. 4:34 So, class Meta and again, model will be 4:36 models.MultipleChoiceQuestion and 4:40 our fields. 4:49 We got a couple of extra fields. 4:51 So we'll do order, prompt, and shuffle answers. 4:52 And there we go. 5:00 So that is, that's our forms. 5:02 And as you can probably see, 5:06 there's not a whole lot of difference between our model forms and 5:09 the non-model forms that we created earlier when we made the suggestion form. 5:13 They're pretty similar, the main difference is that we do 5:21 this ModelForm thing and we do pretty much all of our working class meadow. 5:25 We don't specify the fields ourselves. 5:30 Django will take our fields from our models and 5:33 turn them into the appropriate fields for a form. 5:36 So we don't have to worry about that ourselves, which is really, really nice. 5:40 I'm sure you can already see how model forms save us a significant amount of work 5:45 and coordination. 5:49 We don't have to make sure that our form fields and 5:50 our model fields line up or, anything like that. 5:52 One thing that you might be tempted to do, and I already talked about this a little, 5:56 is to use exclude to leave off fields that you don't want, 6:00 instead of using fields to include only the fields that you do want. 6:03 This isn't the best idea though, 6:07 because any field that's not listed in exclude will be automatically included. 6:08 So you add a new field to your model, and bam! 6:13 There it is in the form. 6:15 By using the fields attribute, 6:17 you keep your forms stable until you explicitly add the new field to the list. 6:18 And remember, explicit is better than implicit. 6:22 Okay, now that we have a couple of model forms created, 6:26 let's see about using them in our views. 6:29
You need to sign up for Treehouse in order to download course files.Sign up