Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Now that we have a suitable abstract base class, let's make a model to represent another type of `Step` in our app.
We used some pretty advanced bits of Python in there so some links would be handy, huh?
-
0:00
In the last video we turned our existing step model into an abstract model and
-
0:05
then created a text model that inherited from step.
-
0:08
Thanks to djangos migrations we didn't even lose any of our data
-
0:11
just moved it to another table.
-
0:13
We need a new kind of step though we have text steps but
-
0:17
we want to be able to make sure people are learning what we're teaching so
-
0:20
we should provide some sort of testing mechanism.
-
0:22
Let's make quizzes.
-
0:24
So we want to make another type of step, I want to make a quiz step.
-
0:29
So we're gonna come down here, and we're gonna add a new
-
0:34
class named Quiz, which will be a step.
-
0:39
And let's get the content into the quiz.
-
0:44
And quizzes won't have content like a text step will.
-
0:48
I think the only thing we want to know for quizzes,
-
0:50
we want to know that a quiz has enough questions.
-
0:53
So let's have a total_questions field.
-
0:58
And we'll do that.
-
0:59
And you know what, let's give it a default of we'll say 4.
-
1:02
It has to have at least four questions.
-
1:05
Not terrible all right so now let's go ahead and migrate that oops I have
-
1:11
to spell default correctly all right let's go ahead and make the migrations for that.
-
1:18
Python manage.py make migrations courses.
-
1:24
And that's gonna create the quiz model python manage.py
-
1:29
migrate courses that's going to apply the migration so
-
1:34
cool now we have those and let's stick it into the admin.
-
1:39
So we'll import quiz and let's just register it,
-
1:44
like I said before, this inline is not going to
-
1:49
make less and less sense as we go along.
-
1:54
So, we probably want to strip that out and just use these registered things, or
-
1:57
build what we're gonna build in a little bit, but, let's go back over here and
-
2:01
let's go look at course.
-
2:02
Oops, sorry, I've got to run my server.
-
2:07
There's the server.
-
2:08
All right, refresh.
-
2:12
And now we have Q-U-I-Z-S.
-
2:15
That is not at all how you pluralized the word quizzes.
-
2:19
Jango doesn't know how to pluralize the word quizzes.
-
2:21
Jango just adds an S to the end of everything.
-
2:24
So let's tell it how to do that.
-
2:26
class Meta: verbose_name_plural would be Quizzes.
-
2:34
Save that.
-
2:36
Refresh this.
-
2:37
And now quizzes is spelled correctly.
-
2:39
That's a handy little trick to know.
-
2:41
Depends on how much you use the admin.
-
2:43
But being able to quickly go,
-
2:45
that's not what that's actually called is really useful.
-
2:48
This is also a thing that you can apply internationalization for.
-
2:51
You can apply translations, too.
-
2:53
So if you need to have it in the French version and the German version and
-
2:56
the English version or whatever, you can totally do that too.
-
2:59
All right, so now the other thing we need to do to make our new models all happy and
-
3:06
copacetic Is we need to be able to view those on the front of the site.
-
3:11
So like if we go to courses and 1,
-
3:16
we got Python Basics, none of our stuff is showing up now.
-
3:22
Make sure we have it all in there, cuz we should, right?
-
3:27
Yeah, okay.
-
3:29
So let's go look at our views real quick.
-
3:37
There's our course detail, and that sends our course through.
-
3:41
And we go look at our Templates > Courses
-
3:45
> step_detail, that's what we want,
-
3:49
oh not step_detail sorry, course_detail.
-
3:56
And step_set see there's a problem.
-
3:59
We don't have a step_set anymore.
-
4:02
We need to get all of our stuff.
-
4:05
So let's come here, and let's add in
-
4:11
our view, we're gonna have to do a little Pythony things here.
-
4:17
I'll do my best to walk through all of this, but
-
4:18
I will also put notes in the teacher's notes.
-
4:22
So we're gonna import a function called chain.
-
4:26
And let's just do from.importmodels which
-
4:31
means we'll have to do some updates here models.
-
4:44
Models dot but that's gonna change.
-
4:47
Right?
-
4:48
So we're gonna ignore this view for right now.
-
4:50
But let's just look here at this course detail.
-
4:52
So what we wanna do is we wanna get all the steps, quizzes and
-
4:57
text that relate to a course.
-
5:00
And we want to combine all those so that they're sorted by their order attribute.
-
5:05
So we're gonna call this steps and it's gonna be sorted,
-
5:09
which sorted is a Python function for sorting things.
-
5:13
And what we're going to sort is the chain, or the combination of all these iterables.
-
5:18
And we're gonna do course.text_set.all and
-
5:23
course.quiz_set.all.
-
5:26
So what chain does is it goes through and it takes the first thing,
-
5:29
it takes all the things from text set and it takes all the things from quiz set and
-
5:33
it puts them all into one big iterable together.
-
5:36
And then sorted goes through and
-
5:37
sorts all those by whatever we tell it to sort it by, which is gonna be this.
-
5:42
Key, which is a lambda, or an unnamed function for a step, and we're gonna sort
-
5:47
it by, you know what, let's call it step, and we're gonna say step.order.
-
5:53
So whatever that order is on each thing.
-
5:57
All right and then we're gonna send out course, but let's also send out steps.
-
6:06
We'll call that steps.
-
6:08
All right, I know that's a lot of Python, we will handle all of that really soon.
-
6:13
And let's go a step further here and we're gonna say text_detail.
-
6:20
We'll leave that the same, but we'll call this text, cuz that's what it is.
-
6:28
And then let's do def quiz_detail.
-
6:31
I copied all that right, yeah, there we go.
-
6:37
Quiz_detail, and we're gonna get models.Quiz.
-
6:43
And I'm gonna do some updates on this HTML in between here and
-
6:45
the next video, but we'll talk about the rest of that in just a second.
-
6:51
So we have those two.
-
6:52
Let's go to urls.py, and let's take our step_detail here.
-
6:59
And what we wanna do is we wanna do a t, and
-
7:04
this is gonna go to text detail, name will be text, sure.
-
7:11
Then we'll do a q, and quiz detail, and this will be called quiz.
-
7:17
And the reason we're doing this is because I want to be able to show you,
-
7:24
Oh we have to update that template.
-
7:26
Not this one, but this one.
-
7:29
And we'll say for step in steps.
-
7:36
And this will have to be, We can't.
-
7:43
See we can't fill this out.
-
7:45
So let's fix that.
-
7:46
Let's go over here to our models.py.
-
7:49
And what we need to do, is we need to bring in our reverse function.
-
7:57
So, from Jango.core.URLresolvers.
-
8:04
Import reverse, and then for text and
-
8:09
quiz we both need to specify a new method named get absolute_url.
-
8:15
And what this does is we return a url, or a url string rather.
-
8:23
So, we're gonna return reverse ('courses:text'
-
8:28
cuz that's the name of our url.
-
8:32
And we're gonna specify the kwargs.
-
8:34
So the kwargs are course_pk, which is equal to self.course.
-
8:40
And you can do course.pk, or you could do self.course.id, which requires
-
8:45
one less lookup as far as the database goes, so kind of handy to do that.
-
8:49
And then step_pk, which'll be self.id or self.pk.
-
8:55
We'll do self.id.
-
8:59
And let's just copy this.
-
9:00
Cuz there's no reason to type it again if I don't have to.
-
9:07
Paste it in.
-
9:08
Gonna add another blank line.
-
9:11
And instead of courses:text it'll be courses:quiz.
-
9:16
So there's our two URL's, and in course detail right here instead of spelling out
-
9:24
all of that, step.get_absolute_url.
-
9:29
And if we come over here and refresh our page, there's our steps.
-
9:34
That was a lot of work, I know.
-
9:36
We added the quiz model, we did this chaining and sorted thing.
-
9:41
Which I'll walk through that in the teacher's notes.
-
9:43
And then we also had to add the reverse URL kind of thing.
-
9:47
The get URL thing.
-
9:49
These are all steps that you're going to do a lot when you're dealing with models,
-
9:53
especially abstract or multi-table inheritance models in Jango.
-
9:58
That verbose name plural is really handy for
-
10:00
making things read a little nicer in the admin.
-
10:03
We're not going to spend much time in the admin in this course though.
-
10:06
Okay, let's go make two more models, one for questions and one for answers.
You need to sign up for Treehouse in order to download course files.
Sign up