Multiple Choice and True/False Questions3:53 with Kenneth Love
It's time to look into Django's other inheritance scheme, multi-table inheritance. Let's diversify our questions a bit!
Multi-table inheritance, or MTI, is something you probably want to avoid most of the time. Our use of it here is solid and appropriate, but we could have solved the same issue by making the
Question model abstract.
If you want, you can read more about MTI
When we extended our Step model, we made an abstract and 0:00 then extended it with our Text and Quiz models. 0:03 This was great, 0:06 because we didn't need another model in the database, our step table. 0:07 But I think it makes more sense to have one question table. 0:10 And then a new table for each of multiple choice questions and true false questions. 0:13 This type of inheritance, where we have more than one table in the database, 0:19 is called multi-table inheritance or MTI. 0:22 Let's take a look at this in Workspaces. 0:26 So we have our Question model, which is a very generic question. 0:28 It just has a prompt and it will have answers that are associated with it. 0:32 Even though I may not always have something specific to a certain 0:36 type of question, I wanna be able to put in multiple types of questions. 0:42 So I wanna have a multiple choice question, and 0:48 I wanna have a true false question, maybe I'd have a fill in the blank. 0:51 Yeah, I don't even know what other kinds of questions, I could have. 0:56 So I wanna be able to represent each of these in the database and 0:59 in it's own particular way. 1:02 So, let's do that. 1:04 So let's a MultipleChoiceQuestion and this is going to inherit from Question. 1:07 And now Question is not abstract. 1:10 We did not put that abstract = True inside Question's Meta. 1:14 So Question will have a table and MultipleChoiceQuestion will have a table. 1:17 And every time we look up a MultipleChoiceQuestion, 1:22 we'll work our way back to the actual question through a foreign key. 1:24 But we don't have to specify that foreign key, 1:28 Django knows there's one because this one is an abstract and this one inherits. 1:30 So let's add in shuffle_answers. 1:35 And this will be a BoleanField and the default will be False. 1:40 So by default we don't wanna shuffle our answers. 1:47 And then let's add in a TrueFalseQuestion, which is the question. 1:51 And you know what, I don't have anything special I want to do in that model. 1:57 That model's very, just plain. 2:03 You know what, I just realized I was about to 2:05 do something I told you not to do a minute ago. 2:08 Let's [LAUGH] do our migrations. 2:11 So there's our multiple choice. 2:18 And then we'll uncomment our TrueFalse and 2:19 make a new migration for TrueFalse. 2:24 And then we'll migrate. 2:28 Cool, ran both of those. 2:30 Run our server. 2:33 All right we're back up. 2:35 So now if we look at this we still just have question, and 2:39 if we were to just add a question, there's nothing special here. 2:42 So what we wanna do is we wanna go over here to admin. 2:47 So remember, we're gonna keep putting all these things in. 2:52 And we should be able to just do MultipleChoiceQuestion, 2:54 And TrueFalseQuestion. 3:03 Let's see what that gives us in the admin. 3:07 So I'm gonna add a multiple choice question. 3:09 And you can see I still get the Quiz, and I still get the Order, and 3:12 I still get the Prompt, and I still get the Shuffle answers, my new one. 3:15 I don't have to go and make a question, and 3:20 then go make a multiple choice question. 3:21 I don't have to keep going back and forth between those two. 3:23 So that's a really nice thing for the admin. 3:27 Many Django developers will warn you, appropriately, not to use MTI. 3:31 In fact, you can include me in that list. 3:35 But the caveat there is to not use it when you don't absolutely need it or 3:38 can't do it cleanly. 3:42 Our approach here, though, is a good, safe approach. 3:43 Okay, I think our models are in good shape. 3:47 So let's look at the major type of Django form known as a model form. 3:49
You need to sign up for Treehouse in order to download course files.Sign up