Django Model Inheritance Options Introduction - ORM Part-9

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to the django rm series part nine so in this tutorial i'm going to give you an introduction to model inheritance so this would be a quick as it suggests introductory guide to model inheritance we're going to cover the three options here abstract models multi-table model inheritance and proxy models so if you haven't started looking at utilizing these when you're creating your models or you're not too sure what this is then this tutorial is for you okay so as i've already introduced django has three inheritance options abstract models multi-table model inheritance and proxy models so i'm going to give you an example for each one of these just a simple example just for you to get the flavor or the general overview of where you might want to utilize this and how you might construct this within your models so straight across to the abstract models so here you're going to create multiple tables the first table is going to be your abstract base class abc abstract base class so from your abstract base class you'll define certain fields and the idea here is that you then create child classes and which you connect to your abstract based class and those child classes will inherit all of those different fields from the abstract based class so the abstract base class doesn't get made in your database um it's simply there for reference for other child classes which are connected to the abstract based class to utilize those fields and it's those child classes that then get created in the database so this is better understood of course by giving you an example so let's go ahead and create an abstract model so there's nothing extraordinary here really the only difference here is that we're going to just define this model that we build as an abstract model so let's go ahead and let's just build a class we're just going to call this base item here and then we can go ahead and add some fields so remember these fields that you build these are the fields that are going to be utilized by the child classes or sorry copied across to the child classes and it's those child classes were actually then created in the database so for example you're going to use an abstract class then when you know for example you've got multiple fields that can be utilized or would be copied or needed in multiple other models so ultimately here this is just going to save you a little bit of time actually creating these fields or replicating these fields into multiple tables so let's go ahead now and just finish this off so we're going to set the class meta and that's where we define the abstract class so we define abstract as true and that then creates this abstract class here and then we can go ahead and add other metadata in actual fact within the class and i'll talk about that shortly and how that gets inherited so now we're going to just think about the other classes or the other models that we need to build for our tables that are going to inherit these attributes here these fields from the base item so let's just put that meta back in apologies so here we're just going to now just go ahead and create some more tables that we want to use or some models now i do apologize it can be easier sometimes to understand the theory when we use kind of uh naming systems that kind of give some context here but so the item a is just referring to a different model that you want to build or want to extend and include these fields within this model here that you're going to build item a so we'll go ahead and you notice here what the funny thing here is that of course when you build the other models you're going to continue putting the unique fields that are related to those particular models and also your meta items um but of course like i said i think i keep reiterating these here are going to be inherited within this class so notice here that we um we're not referring to models dot model here we're just going to be directly inheriting from the class base item in each item here so we can go ahead and just add some more models that you might need and there we go so that's kind of a simple example so item a b c and d they are all going to inherit the fields of this base item so let's go ahead and make migrations and migrate and let's just see this in the database so having migrated the data make migrations migrated if i now just head over to my database so i can open up my database i've got the uh the sqlite module here or extension here installed so i can then just go down and have a look so notice here if i open up my model uh you can see that we've got item a b c and d and also base item the base item isn't built but item a b c and d have all been created now if i go into each one of these models here you'll notice that we've inherited title created an update from our base model here and that's the same them for all these models so this is a good time to point you towards now the django documentation so here in 3.1 topics db models that's currently where we are you'll find some more information about abstract based classes a similar example here now something that i didn't mention uh about meta inheritance so when an abstract um base class is created django makes a meta inner class you declared in the base class available as an attribute okay so if a child class does not declare its own meta class it will inherit the parents meta okay so if we want to override the med meta let's just go back into our example you can see that if we wanted to override we can then just extend from the base meta item so in this case base item and then meta and then we can start adding our own metadata so that's a way of kind of extending the existing metadata from the base item class so just to summarize apologies one more time the abstract model here create a base class those fields are going to be then included in any other child classes that are made and connected to the base class so let's move ahead to the multi-table model inheritance so let's look at this in comparison to the abstract model so with the abstract model we have an abstract class the abstract base class and that doesn't get included in the actual database so here a multi-table model inheritance here the difference is that every model is a model all by itself and what will happen is that django behind the scenes will be building one-to-one links created between the different models this one-to-one field that is created in this link connection between the two models or more that's going to allow us to query both tables for the same data for example so let's just look at the mechanics of this to identify the differences here so let's just go ahead and now build a new model so i've got one pre-made so i'm going to create a class here called books so you can see here we're going to have title and graded those are the fields that we're going to use now what i don't need to do here is define any meta like we did previously with the abstract model to declare the fact that here we're going to be using a multi-table model inheritance setup all we do here is we create a new class and instead from extending from models.model our second model here is just extending from books so this is how we're going to build a multi-table model inheritance so what's happening here is if i were now to migrate this django will create two new tables books and isbn and in the isbn table we will then see a new one to one field created automatically and we have a one-to-one link between these tables and that will obviously mean that we will be able to query either table for the same information so let's just go ahead and run the migrations so we just refreshed the database now we've created some more tables and straight away we see we have two new tables here books and isbn so there's no sharing of tables here like the previous example so what we defined here the field sorry here in each of these classes that's what's now appearing within the table in the database here so notice that i said earlier we automatically create this primary key so django sorry has made a primary key behind the scenes and it's defined as books underscore ptr id and that's going to be a connection to the id field of the books table so here we're making like i said a one to one field or one to one connection so behind the scenes we've seen that our new field is created automatically we can go ahead and override that by just defining our own field for example so this is what was created we could just rename this for example so we're creating a one to one field so if you're not too sure about one to one fields again just have a look at the django documentation first you will find in the documentation a few different examples of one to one being utilized but if you are new to databases it'd be well worth your time actually looking at different relations that you can have between tables so one-to-many one-to-one and so on and that's really going to give you a better idea how to model your data in your database so back in our example here you can see that how we're creating this connection is we're utilizing the parent link so this is really the key that uh defines this uh relation that we have between the isbn and the books class or models here and that's set to true so if you are completely new i i asked you just to focus on the differences or just uh yeah at the start just to focus on the difference between this type of setup and what we saw previously the abstract and that will give you uh at least the initial start to consider utilizing something like this when you're building your models so let's head over to the third option here proxy models so let's provide a comparison again so we have abstract models so that allows us to reuse fields within multiple tables we have the multi-table model inheritance that allows us really to build one-to-one links between tables just to kind of summarize what django automates for us and then the last type of setup here or option is proxy models so here we're focusing on the python so not necessarily the model so when we want to start maybe extending the functionality or adding functionality uh to a model obviously we can do that within the model space but we might want to use a proxy model to find to to define some additional behavior for that particular model that we can utilize within our within our application so generally what's going to happen here is our proxy model isn't actually going to be created by the orm and it's not actually going to appear in the database here we're thinking about behavior here we're thinking about python rather than the model and making like i've already said behavioral changes or fun extra functionality that we can perform on the same model as the model itself so let's go ahead and just show you a simple example so the proxy model starts off with a simple class so here we've got book content for example and then we just got the fields title and created so just a standard table standard affair now like i said in this class we could define different properties we could define different functions etc for this model now there may be times eventually where you want to add some additional functionality that's different from what's defined here within the model here so for example multiple functionality and sometimes that can be better declared within a proxy model so we can go ahead and create a a new model here and notice that we're defining or we're inheriting from the book content and then for example maybe we want to add an additional object manager for example so we could define that within our proxy model and then we go ahead and add some meta so our proxy model here will need to be defined within the class meta proxy equals true and then we can go ahead and for example create some ordering so we can add some other meta here that's different from our original class and again that's just adding extra functionality so we have the option of accessing this data in the book content but we're just adding new functionality here so we're almost separating um the this code here from the logic that we want to include for this particular model so we can go ahead and for example add some different functions etc so you get the general idea this is all about functionality in our proxy model and we can leave the the fields etc and set up within the class here so yeah it's pretty uncomplex really we're just providing a different interface if you like for the same underlying database and i think that really is the key we're just creating or providing a different interface for the model so just going ahead and migrating this i think we're going to cause a problem because we don't have a manager to find so i'll just remove that for now i'm just go ahead and migrate so we've migrated let's just refresh just to show you the fact that our proxy model isn't created so our book content is created you can see that book orders you can see that there isn't a model available for that for that in our database so the book orders proxy model does not get created there we have it so abstract models multi-table model inheritance and proxy models so i do hope there was some value there if you're completely new to this type of content hopefully now you've got a general idea of how to utilize these type of techniques or options within your models again i apologize for kind of any real kind of concrete solid examples again it was just about the setup becoming familiarized or familiarizing yourself or pointing you in the right direction for you to now think about utilizing these options thank you very much and hopefully i'll see you in the next tutorial
Info
Channel: Very Academy
Views: 6,728
Rating: undefined out of 5
Keywords: inheritance, model, django raw, raw method, django sql, django return data, django orm, orm, django, django tutorial, djangotutorial, django tut, django 3, django examples, learn django, django beginners, beginners django, django framework, django 2020, django queries, django database, django how to
Id: 4Xag2FzmN60
Channel Id: undefined
Length: 15min 39sec (939 seconds)
Published: Wed Nov 18 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.