1 00:00:00,000 --> 00:00:04,579 [MUSIC] 2 00:00:04,579 --> 00:00:08,865 When you give an app a model, it's likely going to want a form for editing it. 3 00:00:08,865 --> 00:00:11,040 Django's admin automatically creates forms for 4 00:00:11,040 --> 00:00:14,320 creating and editing instances of our models once we register them. 5 00:00:14,320 --> 00:00:17,460 But what if we wanna do it outside of the admin? 6 00:00:17,460 --> 00:00:21,880 Django provides us with a special type of form known as a model form. 7 00:00:21,880 --> 00:00:24,710 A model form is basically an automatically generated form 8 00:00:24,710 --> 00:00:26,600 based on a particular model. 9 00:00:26,600 --> 00:00:29,870 They provide a great easy way to create new model instances and 10 00:00:29,870 --> 00:00:31,500 to edit existing ones. 11 00:00:31,500 --> 00:00:34,360 This means we get all of the goodness of forms without having to write 12 00:00:34,360 --> 00:00:36,520 an entire form ourselves. 13 00:00:36,520 --> 00:00:40,380 So we have our models and we've already looked at doing standard forms, but 14 00:00:40,380 --> 00:00:42,090 let's look at doing model forms. 15 00:00:42,090 --> 00:00:45,450 Model forms are forms that are made off of our models. 16 00:00:46,630 --> 00:00:51,990 So we want to do this basically the same way that we did our suggestion form. 17 00:00:51,990 --> 00:00:57,277 So that means we want to come here and we want to add a new file named forms.py. 18 00:00:57,277 --> 00:01:02,780 And in forms.py, we of course want to do from django import forms. 19 00:01:03,900 --> 00:01:07,190 So far, so exactly the same as it was before. 20 00:01:07,190 --> 00:01:11,592 All right, so now we need to bring in our models, so we'll say from. 21 00:01:11,592 --> 00:01:18,560 import models and now let's go ahead and make our model forms. 22 00:01:18,560 --> 00:01:20,320 Let's make a quiz form first, 23 00:01:20,320 --> 00:01:25,230 this'll show pretty much all the stuff that we're going to do with a model form. 24 00:01:25,230 --> 00:01:30,300 We'll say quiz form and this is forms.modelform, 25 00:01:30,300 --> 00:01:33,450 thats the one difference right there, or,the first one rather, 26 00:01:33,450 --> 00:01:37,258 is that we're going to do model form instead of just form. 27 00:01:37,258 --> 00:01:41,927 And then we're not going to specify any fields because we're just going to use 28 00:01:41,927 --> 00:01:46,679 the fields that the model gives us but we're gonna have to specify a class meta. 29 00:01:46,679 --> 00:01:50,922 So in the class meta we have specify two things, 30 00:01:50,922 --> 00:01:56,334 we have to specify the model which is gonna be models.quiz, 31 00:01:56,334 --> 00:02:01,531 and then we have to specify fields and so what fields does, 32 00:02:01,531 --> 00:02:07,580 there's two things here, and I'll talk about both of them. 33 00:02:07,580 --> 00:02:11,530 So there's fields, which we specify the fields that we want to include. 34 00:02:13,100 --> 00:02:17,180 So, for our quiz, we want to include title, description, order, and 35 00:02:17,180 --> 00:02:19,860 total questions, cuz those are always there. 36 00:02:21,290 --> 00:02:23,789 The opposite of this is exclude. 37 00:02:25,789 --> 00:02:29,521 And if we were doing this with exclude instead of fields, 38 00:02:29,521 --> 00:02:32,980 let's come over here and look at this quiz. 39 00:02:32,980 --> 00:02:39,260 So we have title, description, order, course, and total questions. 40 00:02:40,470 --> 00:02:44,230 So, we might just say okay, I'm going to exclude course. 41 00:02:44,230 --> 00:02:48,050 Because course is the one field that I don't want to exclude. 42 00:02:48,050 --> 00:02:49,870 This isn't a great idea, and 43 00:02:49,870 --> 00:02:53,960 I'm gonna cover that in just a little bit on why that's not a great idea. 44 00:02:53,960 --> 00:02:56,410 But just so you know that it's there. 45 00:02:56,410 --> 00:02:59,640 What it does is it basically leaves off that field. 46 00:02:59,640 --> 00:03:01,760 That sounds like a great idea, but there's a little gotcha. 47 00:03:01,760 --> 00:03:02,730 We'll talk about that in a minute. 48 00:03:04,458 --> 00:03:11,760 All right, so lets just do our fields and our model right there. 49 00:03:11,760 --> 00:03:16,540 Lets go ahead and actually make a couple more classes. 50 00:03:16,540 --> 00:03:20,740 Let's do a, I don't think we want to do a question form because we're 51 00:03:20,740 --> 00:03:24,100 not going to answer, or we're not going to create questions directly, 52 00:03:24,100 --> 00:03:26,280 we are going to create true false questions. 53 00:03:26,280 --> 00:03:27,920 Or multiple choice questions. 54 00:03:27,920 --> 00:03:31,760 So let's create a TrueFalseQuestion form. 55 00:03:34,340 --> 00:03:38,250 And feel free to use shorter names on these if you want. 56 00:03:38,250 --> 00:03:44,180 I tend to like really verbose names, as you've probably noticed. 57 00:03:44,180 --> 00:03:47,840 So if you want something that's a little bit more terse, feel free to do that. 58 00:03:49,250 --> 00:03:52,900 And we're going to do TrueFalseQuestion as our model. 59 00:03:52,900 --> 00:03:56,370 And for fields we want to do order and 60 00:03:56,370 --> 00:04:00,780 prompt, cuz those are the only questions we have for true false. 61 00:04:00,780 --> 00:04:03,617 Or the only options we have for true false. 62 00:04:03,617 --> 00:04:05,531 We might want to change our true false model. 63 00:04:05,531 --> 00:04:09,814 I'm not going to do it in the course but you might want to change it so 64 00:04:09,814 --> 00:04:14,390 that you can just mark on the question if it's true of if it's false. 65 00:04:14,390 --> 00:04:15,140 Totally up to you. 66 00:04:16,580 --> 00:04:23,160 And then for our last one we'll do multiple choice question form. 67 00:04:25,230 --> 00:04:26,850 Oops, why did I put question form in here? 68 00:04:26,850 --> 00:04:27,619 It's not a question form. 69 00:04:27,619 --> 00:04:29,338 It's a model form. 70 00:04:31,560 --> 00:04:34,070 Forms.ModelForm. 71 00:04:34,070 --> 00:04:36,700 Just not thinking things through today. 72 00:04:36,700 --> 00:04:40,105 So, class Meta and again, model will be 73 00:04:40,105 --> 00:04:49,680 models.MultipleChoiceQuestion and 74 00:04:49,680 --> 00:04:51,130 our fields. 75 00:04:51,130 --> 00:04:52,360 We got a couple of extra fields. 76 00:04:52,360 --> 00:04:58,850 So we'll do order, prompt, and shuffle answers. 77 00:05:00,920 --> 00:05:02,040 And there we go. 78 00:05:02,040 --> 00:05:06,900 So that is, that's our forms. 79 00:05:06,900 --> 00:05:09,520 And as you can probably see, 80 00:05:09,520 --> 00:05:13,989 there's not a whole lot of difference between our model forms and 81 00:05:13,989 --> 00:05:21,188 the non-model forms that we created earlier when we made the suggestion form. 82 00:05:21,188 --> 00:05:25,520 They're pretty similar, the main difference is that we do 83 00:05:25,520 --> 00:05:30,120 this ModelForm thing and we do pretty much all of our working class meadow. 84 00:05:30,120 --> 00:05:33,180 We don't specify the fields ourselves. 85 00:05:33,180 --> 00:05:36,630 Django will take our fields from our models and 86 00:05:36,630 --> 00:05:40,530 turn them into the appropriate fields for a form. 87 00:05:40,530 --> 00:05:43,760 So we don't have to worry about that ourselves, which is really, really nice. 88 00:05:45,420 --> 00:05:49,550 I'm sure you can already see how model forms save us a significant amount of work 89 00:05:49,550 --> 00:05:50,850 and coordination. 90 00:05:50,850 --> 00:05:52,860 We don't have to make sure that our form fields and 91 00:05:52,860 --> 00:05:55,080 our model fields line up or, anything like that. 92 00:05:56,520 --> 00:06:00,370 One thing that you might be tempted to do, and I already talked about this a little, 93 00:06:00,370 --> 00:06:03,660 is to use exclude to leave off fields that you don't want, 94 00:06:03,660 --> 00:06:07,430 instead of using fields to include only the fields that you do want. 95 00:06:07,430 --> 00:06:08,970 This isn't the best idea though, 96 00:06:08,970 --> 00:06:13,490 because any field that's not listed in exclude will be automatically included. 97 00:06:13,490 --> 00:06:15,740 So you add a new field to your model, and bam! 98 00:06:15,740 --> 00:06:17,380 There it is in the form. 99 00:06:17,380 --> 00:06:18,760 By using the fields attribute, 100 00:06:18,760 --> 00:06:22,840 you keep your forms stable until you explicitly add the new field to the list. 101 00:06:22,840 --> 00:06:25,020 And remember, explicit is better than implicit. 102 00:06:26,610 --> 00:06:29,040 Okay, now that we have a couple of model forms created, 103 00:06:29,040 --> 00:06:30,650 let's see about using them in our views.