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
Django models can be inherit from each other in two ways. Let's look at one of them: abstract inheritance.
You'll likely find yourself using abstract inheritance more than any other kind. It's cleaner for the database, easier to wrap your head around, and generally produces faster, nicer queries.
All that's required to make a model abstract is to put abstract = True
in the model's class Meta
.
-
0:00
[MUSIC]
-
0:04
[SOUND] Most of this stage is going to concentrate on area of models
-
0:07
that we haven't talk about before, inheritance.
-
0:10
If you played around with object oriented Python, which I'm assuming you
-
0:13
have because you've come this far, then this should be pretty comfortable to you.
-
0:18
Model inheritance lets us define some fields, methods, attributes, etc, in
-
0:22
one model, and then reuse those in another model without having to repeat them again.
-
0:27
Django models really are just Python classes.
-
0:31
Where this matters, though, is your actual database.
-
0:34
Sometimes you want more tables, sometimes you don't.
-
0:37
Let's start with model inheritance that doesn't make a new table.
-
0:40
We call this abstract inheritance, and
-
0:43
we call the models that don't have a database table abstract models.
-
0:47
Django has two main types of inheritance, abstract and multi-table.
-
0:52
Let's look at abstract first.
-
0:54
In abstract inheritance, we have a model that's marked as abstract.
-
0:58
We'll call ours User.
-
1:00
This user model won't actually have a database table and
-
1:04
can't be used for quires.
-
1:05
So how's this useful?
-
1:07
This model can be used for a starter for other models.
-
1:11
Maybe we have Students and Staff.
-
1:13
These are both users but we don't want to have to look up a user and
-
1:16
then a student or vice versa.
-
1:19
Since our User model is abstract, we can make both Student and
-
1:22
Staff extend from User.
-
1:24
Each of them get all of the fields of User and they get their own tables.
-
1:28
Our queries are simpler.
-
1:30
Our database design is nicer.
-
1:31
And our models file is still pretty easy to understand and dry.
-
1:35
Let's go to workspaces and make this abstract concept a bit more concrete.
-
1:40
So, I'm now inside of courses, models.py where all of our models are.
-
1:45
And we need to see about making an abstract model.
-
1:49
And the model that makes the most sense to make abstract at the moment is Step.
-
1:54
We're gonna have multiple types of steps later on, right?
-
1:57
We may have text based steps, we might have video based steps, we're
-
2:01
gonna have quizzes, eventually we'd have code challenges, all this kind of stuff.
-
2:06
So we wanna make it to where this is as adaptable as possible.
-
2:10
So some things are gonna be the same in all of these different types of steps, and
-
2:15
some things are gonna be different step to step.
-
2:18
I think content is one that is not gonna be the same in every singe step.
-
2:24
So it's probably one we would take take out.
-
2:26
First though, let's come down here and
-
2:29
to make Step abstract, we have to say that it's abstract.
-
2:34
So we say abstract = True.
-
2:38
Now, that means that we're basically gonna get rid of the table.
-
2:43
And we just got an error here.
-
2:45
It tried to restart and it's like, oh, you can't have abstract models in admin.
-
2:48
We're gonna fix that in just a minute.
-
2:51
So then, let's take content.
-
2:53
Oops, let's take content out.
-
2:57
And I'm gonna cut that instead of just deleting it,
-
2:59
cuz I need to use it again in a minute.
-
3:02
And let's make our new class.
-
3:06
And so this class is gonna be called Text.
-
3:08
This will represent our text steps.
-
3:10
And since Text will be a step, we're gonna have it inherit from Step.
-
3:16
And then, we'll put our content field back in there, okay?
-
3:20
Cuz we want to have a content field.
-
3:24
And that should be it.
-
3:27
Everything else is gonna come from Step.
-
3:28
We're gonna get this dunder __str__.
-
3:31
We're gonna get the ordering.
-
3:32
We're gonna get all that stuff from Step.
-
3:35
The only thing that's not gonna happen is Step won't be in the database.
-
3:38
So let's save that.
-
3:40
And let's come down here and we're gonna have to make some migrations.
-
3:45
So python manage.py makemigrations courses.
-
3:51
Now, we wanna be sure and we need to pay attention to this one.
-
3:56
So we're gonna do that and, oh, sorry, we're gonna go fix our admin real quick.
-
4:02
Let's go over here to admin, and
-
4:05
let's come down here and let's not register step.
-
4:12
And let's take off that inline as well.
-
4:15
All right.
-
4:16
So now let's give it a try.
-
4:23
There you go, okay.
-
4:27
Cool, all right, [LAUGH] so now this asked us a very important question.
-
4:31
Did you rename the courses.Step model to Text?
-
4:34
And we did.
-
4:35
We want all of our existing steps that we created in Django basics,
-
4:39
we want all of those to still be in here.
-
4:42
We just wanted to move them over to the Text table.
-
4:44
So we're gonna answer y, press Return, and now we get this migration and
-
4:49
we have rename model Step to text.
-
4:52
And I wanna actually look at this migration because this is
-
4:54
an important one.
-
4:56
So if we look here and we look through here,
-
5:00
we see that we renamed the model Step to Text, and that's really all we did.
-
5:05
That's it, because that's really what we wanted to do.
-
5:07
We wanted Step to become abstract.
-
5:08
We don't want step to exist anymore.
-
5:11
But we want all the data that's in Step to move over to this new table called Text.
-
5:16
So now let's go ahead and run that, python mange.py migrate courses.
-
5:21
Press Return, and then now its asking us,
-
5:24
do we still have the content type of Step that belongs to Courses?
-
5:29
And we don't, it's now called Text its no longer called Step so that doesn't exist.
-
5:34
So we're gonna type yes to tell Django to get rid of it.
-
5:38
And then let's go ahead and run our server.
-
5:43
And let's go fix our admin.
-
5:45
All right, everything's up and running.
-
5:48
Close that migration.
-
5:50
And up here inside of Step we wanna bring in Text.
-
5:54
And let's change this over to TextInline, even though we probably wanna get rid of
-
5:58
these inlines and come up with something a little better later on.
-
6:02
And we're actually gonna build a lot of this out as a little CMS in a bit.
-
6:08
TextInline, and we'll register Text.
-
6:13
All right, cool.
-
6:14
And now if we come look at our Site administration, we had Steps.
-
6:18
So let's refresh that, we should now have Texts.
-
6:21
And if we look in our Texts, we still have the couple that we made before.
-
6:27
And if we look at Courses and we look inside here,
-
6:30
we still have the ability to create Texts from inside of the admin.
-
6:35
So everything's still good.
-
6:36
So, let's go look at our model again.
-
6:40
Turning this to True,
-
6:41
all it really did in this case was it got rid of the table that we already had.
-
6:45
Mot of the time your gonna do this without having an existing table.
-
6:48
But it's good to know how to do it if you have a table and you're gonna go back and
-
6:52
refactor, and rework through how things work.
-
6:57
Abstract models are an amazingly handy tool when you need to be able to diversify
-
7:01
your models without creating a ton of tables or
-
7:03
requiring complex joins in the SQL that Django generates for you.
-
7:07
Later in this stage, we'll look at the other type of model inheritance Django
-
7:10
offers, but first, let's start our quizzes.
You need to sign up for Treehouse in order to download course files.
Sign up