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