Question and Answer Models6:28 with Kenneth Love
Quizzes aren't much use without questions, and questions usually need an answer, so let's see what we can do about this.
Since we've used it a time or two, here's more information about
get_absolute_url. I definitely encourage you to provide this for every model that has a detail view.
Our question and 0:00 answer models actually won't need any inheritance, at least not right away. 0:01 This will give us some good practice with foreign keys, though. 0:06 So I wanted to show what I did as far as the HTML and stuff goes. 0:10 So we now have a text detail and a quiz detail. 0:14 If you were to look at a text one, it's still gonna look exactly the same. 0:17 If you were to look at a quiz one, it now just says, quiz questions here. 0:21 It's the same idea, basically. 0:25 Added a quiz in here that belongs to our particular course. 0:28 So, let's go, we need to make a couple of new models. 0:34 You need to make like a question model and an answer model because we have quizzes, 0:37 but we have no questions, and we have no answers. 0:42 So our question model's gonna be pretty simple, actually, for right now. 0:44 We're gonna make it a little more complicated in a bit, so don't get too 0:50 comfortable, but let's call it a question, and we'll say it's models.Model, 0:55 like we've done before, and we need a foreign key back to the quiz. 1:00 So models.ForeignKey(Quiz), and 1:04 we need to know the order for the question, 1:08 like, which question should we ask first? 1:13 And by default, it's gonna be 0, and then prompt. 1:19 It's gonna be a text field, and this is where you type in the prompt 1:24 for the question, right? 1:30 Like the text that shows up. 1:32 So, class Meta, we're gonna say ordering is equal to order, 1:34 cuz we want it ordered by the order. 1:42 And then, let's define a get_absolute_url, and 1:46 I don't wanna have a link to a specific question. 1:50 That seems kind of silly, but I do wanna have a link directly to the quiz so 1:53 we're gonna return self.quiz.get_absolute_url. 1:59 I have to be able to spell all of this stuff, and 2:04 I wanted to find a string method, and I'm just gonna return self.prompt so 2:08 that way, if I wanted to, I could just print out the question and I'm good. 2:13 I've got the thing, so I like to do migrations for each model, for 2:18 each change. 2:22 I know we did a couple of changes before. 2:24 We did the abstract and the moving content to text but 2:26 it doesn't change the database in more than one way. 2:30 It did one change to the database, so for 2:32 this one we're gonna be adding one new model, so let's add a new model. 2:35 So python manage.py makemigrations courses 2:41 Okay, now we don't have to apply it right now, I just like to make it for each one. 2:49 So let's go ahead and make out answer model as well. 2:54 So, answers, [SOUND], 2:57 obviously belong to a question, right? 3:00 So, models.model. 3:05 So our answer should have a ForeignKey back to the question. 3:08 That they belong to. 3:16 If we want to specify the order of the answers then we can, but we don't have to. 3:18 And then we need a text that'll show up in the answer and 3:28 we'll say that it can, at most, be 255 characters long. 3:34 And then, we need an ability to say whether or 3:41 not a particular answer is the correct answer. 3:43 So we'll do a boolean field and we'll default this to false. 3:47 Because by default the answers are gonna be wrong we're gonna specify exactly which 3:51 answer is correct so we'll do class meta again ordering 3:56 equals order you'll find yourself doing that quite a bit and 4:01 we'll define the STR again Return self.text. 4:06 Partly this is because we want it to look nice in the admin, but 4:12 also just because these are handy things to always have on your models. 4:14 You want to be able to pull up your model in Python's, or in Django's shell, 4:18 rather, and be able to get something that makes sense when you look at it. 4:22 You don't want to just see, like, the ID number or the memory address or whatever, 4:26 So let's make another migration real quick, and there's that one, and 4:31 then let's run both of those, python manage.py migrate courses and there we go. 4:36 It applies number 6 and 4:44 it applies number 7, so cool, we should be good to go on those. 4:45 And let's just, we'll go ahead and run our side. 4:51 And we can add them into the admin real quick even though 4:55 I don't think we'll use them in the admin a whole lot. 4:58 Let's change this to that. 5:00 And then we'll do models.Text. 5:05 [NOISE] Let's just take out this end line. 5:07 That end line is stupid. 5:09 We're never gonna use that. 5:10 Which means we don't need this anymore so that's nice. 5:14 All right, and then admin.site.register(models.Question). 5:19 I'll go and add the models into those other things in just a minute. 5:27 Register(models.Answer). 5:30 So this means models., models., models., cool. 5:36 So now, if I go over here to courses and 5:43 I refresh, I should see answers, questions, quizzes, tests, courses. 5:49 They're always in alphabetical order. 5:53 So, whatever. 5:56 There are things you can go to change the order of them, but 5:59 I don't usually find it all that important. 6:02 It's a really good idea to get use to creating that get absolute URL method too. 6:05 It makes it easier to get to specific model instances. 6:09 And in admin we'll use it too To give you a view on site button for each instance. 6:12 And who doesn't like shortcuts? 6:16 So, like I said before, these are simple, everyday utilitarian models. 6:19 But now that we have a solid question model, 6:23 we can diversify it with two new models that both extend question. 6:25
You need to sign up for Treehouse in order to download course files.Sign up