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