Python OOP for Django Class Based Views Explained - Beginners guide to OOP and using Django Classes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to the principles of object-orientated programming and django classes so this is a django class-based views tutorial uh where i will explain some of the principles of python object-orientated principles so we go through classes attributes inheritance methods instantiation but the twist is here that we move this to uh we move this in the direction of django and django classes so we're kind of feeding slowly django classes so you'll get to learn object orientated principles and django classes kind of at the same time here so eventually we get to a point once we go through some of this basic information so mr initial sorry information we'll get to the point where i explain what super is and hopefully at that point you'll have a such a clear idea what super is you'll never need to read or look at any other information ever again to understand how super is being utilized then we move on to overriding class methods so hopefully these principles are going to allow you to understand how class-based views are or the generic class-based views are being utilized in django and this idea of inheritance and multiple inheritance which you'll explain as we go through this of course all feeds nicely into this idea of mixins so i don't really take you through um what mix-ins i don't really give you any examples on mixins at the end i just feed you into the idea of mixins which will lead to another tutorial because the tutorial got quite long at this point so we'll definitely leave it to another tutorial but at this point you'll have a better understanding hopefully what mix-ins are and just on the last point i'd love to get your feedback and feelings and thoughts on this tutorial if you do go through the whole tutorial for example uh to better understand where areas where i might be able to improve upon or change or add to so i can improve this tutorial so before we start this is the code that we're working from this is just code i've downloaded from the build in the blog series you can see that we have some urls so we have this default url for the home page in the views that's connected to this home which is a function based view so you can see that that's going to collect all the data from the database and then return this template and all the data so this is what you're seeing on the page uh this is all the data that's currently in the database there's only one item so only one item is being returned now the reason why i'm showing you that is because all the code i'm showing you in this tutorial it can be written within the uh the python shell for example oh sorry the we can access shell from the manage pi file so shell so here we have the python shell which we can utilize of course you can get into the show in many different ways so all the commands can also be typed into here now typically on tutorials that's normally the case if you read tutorials we utilize the shell to explain the the commands and the code sorry so we're simply just not going to do that here so by utilizing some of this django code here it just gives you a better connection between what i'm going to explain and how django works i would say that learning object orientated programming in python is easy in actual fact so let's go ahead and look at an example of a class so just above the home here we're going to first of all create a class so class and then the name of the class now typically we utilize this capitalize word notation so dog if it was a big dog would have capital b and a capital d and so on so that would be the the normal type of way of uh naming or the naming convention of a class here so if we look down i think there are some classes here for example you can see that convention is actually being utilized in the class as we have in this page so let's just go ahead and build a simple class so i'm going to name this class computer and i'm just going to use a placeholder here the pass so that the code will still be functional if i were to go now and just refresh my page nothing's obviously going to happen so we've just built a class so there are different angles here describing what a class is so in object-orientated programming i would say that a class is a a template a blueprint for creating objects in this case we want to create the computer object so within our class we can create or define the computer for example so if i wanted to model a computer i would need to think about some of the properties or attributes of a computer and then inside of here i can then go ahead and model or define those attributes in the computer so the idea here is this object now i've created this object this blueprint i can now utilize it throughout the rest of my application so for example if i wanted to import this into somewhere here for maybe in this function or if i wanted to use it down here for example i could go ahead and import it in for example like this and we'll explain this in a while and then now i have access to all those different attributes about a computer and then i want might want to perform some sort of action here on that data to produce some new data and return it to the screen for example so that's built in a simple class so now let's talk about class attributes so class attributes are attributes that have the same value for all class instances now that might not make sense but that's going to be developed further in this tutorial so let's just go back and say that class attributes are attributes that we're going to define within this class here that we can then utilize and access if we were to for example the example i gave you earlier where i said and we could include the class right here we could then access those attributes from within this class for example so let's go ahead and just add some attributes so the reason i started off with computer because i wanted to get you into this ideal thought process that when we define class attributes typically these are attributes or we could just say variables or information or data that we set up that provides information about in this case the computer that we can say would be the same for all computers now with a computer you may for example have a cpu it definitely requires a hard drive or storage of some sort and it definitely requires maybe a power supply now with the computer those items although it's the case that the computer will require those components those components will as we probably know will be different so we have a different cpu we might have a different type of power supply and we might have a different type of motherboard and so on so we're class attributes we're looking for an attribute which we know that we can utilize that would define this class er more specifically so this is typically why simple examples of object orientated program normally start with for example animals because if i were to change this into a dog for example now i could say pretty comfortably that um dogs would have uh four legs so remember we're building a blueprint here so if i were to use dogs now somewhere down here in my code i could extract this legs which would equals four now i would know that whenever i use this dog class in any of my code down here the the lex is always going to be four now that's because generally dogs would have four legs right so that is something that is a mainstay to or an attribute to this um this dog here which is the class that we're modeling that we can be sure of and save for definite whereas where we utilized computer for example these parameters here would change so we need to kind of deal with that in a different way so we could probably go ahead and also define other attributes for example eyes so the dog would have two eyes for example so again that is something that we can say would uh be generic for every single dog um and it's not something that necessarily would change so therefore we could create a class attribute for it so now we define some attributes here what we're going to do is access those attributes so this is why we're utilizing this code predominantly so we can just for example use this uh function here to fire up uh different commands so let's go ahead and print so what we can do is we can access this class um via its name in this case it's a dog and then we can go ahead and for example access some of these class attributes by also naming those two so we use the dot and then we can for example print out the amount of legs so that's just going to simply print out the amount of legs a dog has so let's just open up our terminal here move this up a little bit i'll save that and if i were to for example refresh the page now obviously what's happening here is we're firing off this function here the home function so therefore this statement is being fired off too and you can see here that we're getting an output of four so you can see very simply put how to access our class so hopefully you can see that what we're doing is we're modeling something here in this case a dog and we're defining some of the attributes of a dog and it allows us with object-orientated program it allows us to model something but just once that means we've got better code reuse because we don't need to for example if i wanted to define a dog in this function here i would have to go off again and define legs and eyes well in this case i don't need to do that with a class because i can just call the class associated to a dog and then i can just extract the information and maybe do something with it so with that very small piece of information that we've provided so far i think we could probably go ahead now and comfortably change this function-based view into a class-based view so let's just follow the same principles as what we've already done so instead of using def for function we're going to now utilize class so we're going to call this class home and we're just going to remove the request so there we go so we now have a home glass now let's just remove some of this other information here so let's just recap what's happening within this function this view so we're collecting all the data from the database so the post table and we're utilizing this custom manager let's just ignore that for now but we're getting all the data from the database and we're turning it into this all post variable and then what we're doing is we're passing that data across uh into this post context and then we're utilizing this post context to loop out all the information on the home page and of course we're utilizing this template and we're rendering that out so what we're going to do is we're going to remove this and we're just going to follow some of the django principles and practices for creating a class based view so let's just remember this point i was trying to make here we've created a class now at the moment we've just added some attributes but what we're going to also do in this class is add some functionality so we're going to add some functions in here that will perform actions now what we have in django is we have pre-existing pre-generated classes which we can utilize to actually perform operations for us and to perform functions and to actually do things for us without having to code it ourselves and that's what essentially are the django class based generic views now i do have some other tutorials that you may have already seen on class based views and this kind of follows on after those tutorials so maybe you already have an idea of class-based views so this list view up here this is a pre-existing class that's been generated which has access to the functionality um that will basically take instructions in that we provide it and then output that into what will be what we see on the screen here as we had before but with the function view so let's just go ahead and now include this into our class so this is going to be uh list view now what we can say here in actual fact because we're building a class and then also we're connecting this is another class what we're doing here is something called inheritance so this is a process by which one class our class that we defined here takes all the attributes remember these are attributes and methods which we haven't explained yet but methods are basically the functionality within the in the class that perform some actions we're taking all of those attributes and methods and we're then making them available within this class here our home class so all the functionality is somewhere hidden inside here in this abstract kind of class it's already pre-created by django so what we can do now is we can pass information or we can add information within our classes some attributes which will then be utilized in actual fact by the methods within the other classes that we bring in to this class that we inherit into this class so we know that for example template name now this is a um a function so this is a a variable that we can or an attribute that we can set up that we know that this list view here actually utilizes so what we want to do here is actually just to find like we had in our um in our view previously in our function based view we're just going to define the template that we're using for our page and now we just need to define the model that we're using and remember we're using the post model previously in the function i should have just kept the function up so we could see that apologies and then in addition to that remember we passed over the data to the template and we use a very specific name to do that so in our templates in our blog and our index page um you can see the data that we send over we're actually referring to it um as posts so we're sending the data across to this template and we want to refer to that data as post so that's where all the data has been held from database so let's go ahead and set up what is a context object name so that's the context that's the name of the object that's holding data that's being passed across to the to the template so that's going to be uh posts so we've completely changed now the function view into a class based view and we've just added some different attributes now which are going to be utilized by this hidden class that we're just inheriting all the methods and attributes from so let's go ahead and have a look at the homepage again we refresh and you can see that we are receiving an error so i'm not too sure why that is at this moment um i think we've already i thought we've got everything we need here uh template name model and oh it goes okay yes there is one more thing that we need to do of course um so this is okay so now because we're moving from function based views to class based views of course we need to go into the urls and we need to change our url so what we need to do here is we just need to define and tell django that we're utilizing a class based um view so you can see here for example we now add this as uh underscore view and this will become a lot clearer in later tutorials as to what that is i'm not going to explain it right here but essentially what we're doing is we're just defining the fact that we're using a class based view and now we can go back and refresh and you can now see we're just seeing exactly the same page again so maybe at this point i'm going to just uh embellish some of these points i've already made but i just want to make it crystal clear you can see that this list view here is being imported into this page so the code that's connected and inherited within this list view class has been actually made accessible to this page and then we utilize it within this home so within here we have a load of other code so let's just actually uh quickly see what's inside this list view so if we just follow this path django views generic import list view so let's just go ahead and we're inside the django github repository so we know that we need to go to uh views and then generic so we're in django so views and generic so let's go to views and then generic and inside of here we've got something called list view so we're importing list view now you can see that list view doesn't actually exist here but let's go ahead and just go into list so here we go this is the class list view right here so you can see what's happening here that in this class here this list view class well there isn't really too much there right but what's important is that that is important inheriting from these two classes so what we can do now is have a look at this class for example or this class no i was hoping that was going to link me so the base list view so let's go up and have a look at this base list view so you can see here this is a class again so this is another object you can see that it's actually importing more we're inheriting more classes um so you can see there's a chain here happening inside of here we've got some functions so we haven't or methods that we haven't spoken about yet which we'll cover shortly uh but just to point the fact that there's code here it's performing some actions so we're copying by utilizing this list view here we're bringing all that in to this page so we can access it and that's what we are then kind of allowing that code to access these parameters here these attributes in order to perform the actions that we see so for example if i were just to remove this home and then go back to my page obviously it's not going to work because it don't now have all that functionality that actually produces the page on the screen as soon as i add it back you can see that the page will now load maybe in a minute maybe it will load what's going on okay so it's just my code so there we go so we now have everything back so let's just go ahead and now change this for example to animal because uh let's be a little bit more generic here say we want to model cats and dogs now if we want to model cat and a dog we know that cats and dogs they both have four legs so there's no point me making a class for a dog and then to fight for a dog and then defining four legs and then making another attribute in dog with eyes two eyes and then going ahead and then making a new class for a cat and then also then defining legs and eyes because they're all the same right so this is another reason why we'd want to use inheritance because we can then inherit this class into this class here so we take all these parameters and we make them available within dog so let's go ahead and do that so this is um an animal so now we have all this attributes now available in within this class and we can now do the same thing for um the cat so let's uh inherit all those attributes too so let's just use pass as a kind of temporary temporary template there so we can get the code to work so now what we have is this information is now being inherited into these classes so i can access this information from these classes so just let's just go ahead and just show you that type of inheritance so we're going to print out in this case we're going to print out for example cat dot and then legs now here we're specifically calling cat now because these attributes have been copied over to or made available for in this class we can now access how many legs the cat has through this class inheritance so let's just go ahead and refresh the page which is broken again because i haven't added the correct syntax okay and you can already see here we're producing four and yep so the answer is four so we're producing four so we're printing out here the cat's legs uh and actually there's a problem uh let's just go back to that apologies um so let's just do that again come on apologies not very clear refresh okay so it's just here it's four i can guarantee it's working everything is okay uh so we're receiving four so if i were to change this uh to five we can just see that happening um it should automatically happen yep so you can now see that there's five okay so you can clearly see this inheritance chain starting and hopefully now you have a better appreciation of these uh generic base views which are there which of course there are many of in django and how that's been utilized through inheritance through object-orientated practices here inheritance being able to collect and to utilize attributes and what we'll speak about in a minute methods within these different classes so roughly ones or loosely what i'm speaking about here is something called multiple inheritance so just to cover the very basics of object-orientated practices i just wanted to establish this idea of instantiation or instantiating a class so now we have these blueprints for different classes so let's just go ahead and noise i see or i should say sound probably noise because uh sound equals uh say woof okay so now we've established these classes here we have something called instantiation so notice that i'm not actually bringing or inheriting the class here but i'm yet running previously um the different classes here so we are running this print directly from this class here almost so let's go ahead and just talk about instantiation slightly okay so instantiation is basically where we create a copy of the class in memory and then we can access it so for example let's go ahead and say animal so we just create a new variable called animal and we instantiate a class so we create a copy of that class let's just go for uh dog in this case so we just define dog and there we go so we've created a copy of that class into this variable animal here we've instantiated the class and now what we can do is access that class so for example print and this case um animal so through animal i can then go into animal and for example find out how many legs it has so obviously the dog should have four legs remember that's being inherited from animal so um maybe i should use uh a new here so new animal just to make it a little bit clearer so new dog and then this new dog we want to see how many legs it has so let's go ahead and do that you can see we've already got five so let's just change that to four you can see now it's moved to four so we're accessing we're accessing this class this dog class we're making a copy we're instantiating it into this variable storing it there and then we can access the data in this manner so now that leads us into methods so we've spoken about class attributes here so now how about methods so within a dog the dog might perform specific functionality of specific functions um like scratching its head for example so we need to kind of model these within this class so the idea is that we're trying to um create functionality logic and prepare data with inside these classes that are very specific to the class in this case dog and then we can utilize those in other classes here to perform other actions so let's go ahead and create a method so we have this initiation and method initialization method so we use the double and then the init so we've got this dunder method here and then we always take in self to begin with so that's just a generic uh principle practice when we generate a method so we're just taking self and then let's go ahead and uh let's give it a name for example so we're going to create this initialization function so basically when this class is run so when we make a copy of this class this is going to be fired off automatically this function here is going to be fired off automatically because we're using the dunder so let's go ahead now and do something um so let's let's take in the name of the dog so what we're going to do is we're going to pass the name of the dog over so for example less lesser finishes off actually so let's just go ahead and we're going to pass the name of the dog across sorry and then we're going to store it within this method here so let's go ahead and uh do that so what we need to do here is call uh use self and then we're going to set up the variable that's essentially what we're doing here we're setting up a variable called name and that's going to equal name okay so this is the variable name and this is the the data that's been passed over through the method here and we're going to store it right here inside of this new function we're going to call name okay so now we can pass that data across so let's go ahead and just add here a name of a doc so we're just going to call this dog lady okay so that's the name of the dog so that's going that day is going to be passed across over then stored in this function now that's going to happen automatically as soon as i create a new instance of this class because i'm using the init the initialization function is going to print out the name lady and the amount of dogs so let's go ahead and also sorry just do that and print out in this case uh the new news the name of the variable where we're storing we've been instantiated the object dog into new and now we're saying new and then we're going to call out name okay so let's go ahead and see that so you can see that that is working so this initiation function was just important to for you to identify because if you have a look through some of the django code and some examples you'll definitely see that so hopefully now you've got an appreciation of what that is it's a basically a way of running a function straight away as soon as you call the class okay so there is another function also that can be utilized um at the end of this process um so we can perform all these actions and then perform a final action that's something that's very common in object-orientated language as well but what i wanted to show you here also was the dunder string method so let's just go ahead and just print out for example new so what we're doing here is we're creating a copy of this object putting it into new and then we're just going to print it out so if we just have a look at that we just have a look at that and there's a problem with this bit here so let's go and have a look at that and you can see that we're printing out the object uh blog.viewsdog object at blah so that's where it's stored in memory for example so let's go ahead and now get rid of his pass let's uh create our the classic string dunder method um i'm sure we're familiar with this in our model for example so here we're taking self and then we're going to return and in this case just uh just going to say hello there we go so as soon as we run this class remember this string dunder method um this is a string representation of the object so instead of now returning the object name for example we saw previously we can now see we're returning hello so that's just another example of using a a string dunder method there that which doesn't really have any place um in the code but i just wanted to show you that understand establish that even further this string method and what it's trying to do taking this one step further of course you then start to build some other functionality that you want to call within dogs so let's just call this for no particular reason uh hello and then we're going to i just want to show you how to run this function so we're just going to return um let's just return something really simple so self dot name so we're just gonna access the name um so that we can attribute so we can pass it out so that's the hello so let's go ahead now and access it so we create our doc and now we're going to print out for example new dot and now we need to define what we want to access so in this case um the function hello so this is a function so we're going to need the doubles there so we've made sure we've got the double parentheses here now there is a problem still with this piece of code uh i just want you to take a look for a couple of seconds um see if you can spot the error so i did tell you that it's important to utilize self so self basically represents the instance of the class so by using this self keyword we can access the attributes and methods within the class so for example if i want to access uh maybe this in this case you can see that i'm accessing this method here within this function in order for me out in order for me to be able to do that i'm utilizing this self which is going to then allow me to set up this variable which is then going to be able to be utilized throughout this class so if i want to now access it you can see here i'm using self now that isn't going to work because i'm calling self here but this isn't actually going anywhere so what i need to do is i need to pass all this information inside of here or make it available so i have to do that by parsing in self and now because i've done that i can access the other information with inside this class through this self keyword okay so now i can access self name so i shouldn't i should now receive uh the name so let's go ahead and do that so we're just going to refresh the page which doesn't seem to be working on that page so we're just refreshing the page let's go back into our code and now we have it says lady now we were trying to return the name uh which is the case so let's just go ahead and just make sure that our initiation uh oh just double check so yeah we're just printing out hello and that's now printing out the name of the dog so let's just change that very quickly notice that it has now changed to as to blah blah blah so we're now just printing out um this here so what we can also do is we could probably just extend this if we want to so very crudely we just uh use this uh kind of concatenation here so you can see here by returning self.name plus the i am a dog you can see that we're now outputting the name of the dog and also i am a dog and that brings us nicely on to super so super is a question what is super is a question i've been asked a few times now in the community and just about now to answer it hopefully it will be comprehensive enough for you to understand and so i never have to answer this question ever again so super now there's a time where for example here we're accessing what's inside of dock now there may be occasions where for example the sound of the dog might have changed there may be occasions for example where the dog hasn't had an accident unfortunately and now only has three legs or one eye for example so what we're talking about here is overriding the existing attributes and methods with inside of a class now this principle is really important in django because what we do a lot i'm just going to remove this what we're doing a lot here when we're using these generic views sometimes what's happening in this listview what's already been coded inside of this list view class and other classes that are being inherited those attributes and methods maybe they don't they don't suit our program maybe we need to change them slightly so what we need to do is overwrite them so what we're going to do now is just go for a basic example and we'll provide some more examples later of overriding the class so what we're going to do is we're going to take this class uh bring it in as per normal but then we're just going to override some of the properties or um attributes within this class so this is a this is a class so i'm just going to utilize the initialization function um as i call it so let's go ahead and use self so this is just basically going to run when we fire up home class it just makes it easier for us to utilize so what we need to do now is set out a variable so i'm just going to call this variable new and then that's going to equal and this is where super comes in so super allows us to essentially um copy or grab a section of our code from the previous inherited classes so i can specify what i want to access so for example what i'm going to do here is i'm going to take this hello function here method and i'm basically going to override it so i'm going to change it so what i can do is i'll use super to access uh and then i then just define what i want to override so in this case it's going to be hello so supra just provides me a way of accessing in this case the dog method hello now at the moment i can't access it and maybe you can see why i can't access it it's because i've not actually now inherited the class so what i need to do now in actual fact is add dog right here because i need to actually inherit the class so let's go ahead and do that so now dog is available now i can start to think about inheriting so first of all let's just go ahead and print so let's just print out new so we can see what we're actually working with here so we want it to be within the function so it fires off when we find the class so you notice there's nothing here so as soon as i fire that class off i don't know what keeps doing that um in the home page home has no attribute name okay so because i haven't actually sorted that out so let's just uh remove this for now because it's trying to we don't have anything being inserted and the strings we're not gonna need that anymore so we're just gonna start off again but just utilizing this single function here so we're just going to grab that function and just print it out or method so we've still got a name here home object has no attribute name so oh because again we're trying to print out the name so let's just go ahead and let's just just return something simple i am a doc let's just do that okay so now it should work if i refresh there we go so we fired that off so let's just go down here have a look see if something's happened so you can see that it says here i am a dog now apologies that we're doing it this way um it can be a little bit messy hopefully you can see what's happening we've fired off this this class by running that page now because we're using the the init dunder method here it means that this also has run when we've um requested this page run this class so now you can see it's printed out new so what we've done is we've overridden or we haven't over in yet but we've collected this method here and put it into this variable and we've just printed it out and we've done that through super and the fact that we're inheriting this class into our home class here now i didn't set this up very well but hopefully you can start to appreciate because we'll just stop here hopefully you can start to appreciate now we have access to this function anything within this function we could potentially now overwrite and return that instead now like i said i'll provide you another example shortly but i just wanted to show you those principles there of super so this is where it gets a little bit more interesting so we'll start to now feed this into django now let's just get rid of uh cat for now um and we don't necessarily need animal so let's just start to kind of make it a little bit more realistic um so with inside of here i'm just going to call this uh db for database and just get rid of sound for example and i'm just going to call this uh just for now django okay so we're going to bring in django into our class inherit this so we're inheriting db so let's go ahead now within this function let's go ahead and actually utilize and to actually create a database set or a database object so let's just call this data and this is going to equal self dot model dot objects dot all now just take a look at what's happening here i'm calling self.model in the django so i'm calling this attribute called model it doesn't actually exist within this class but notice it does exist here so when we're running list view remember we defined the template name the model that we want to run and collect data from and then the context so whereas before i've mentioned basically collecting data from the home so i've mentioned a kind of a data flow whereby we're collecting data and putting it inside of home so we're collecting data from other classes that we inherit and moving it into the home class here we can also do the other way around so i say this very loosely the other way around but we can use this data now to be utilized within these inherited classes in these parent inherited classes so self.model is actually referring to this model data here so i'm actually here is going to be changed to the word post or the post object sorry it's going to be utilized here so we can actually run a database query on the database but it's defined in the home so let's go ahead and do that and we're going to return data so what's going to happen here is that when we run this function we're going to use this data here to be to run a query on the database so we're using self-talk model and then we're just going to print it out so we don't need that anymore so let's just go ahead and print out oh sorry we will we'll use that so because we need to access so we're going to access the the function here and put it into new and then what should get printed out is obviously all the data from the database because all the data from the post model in our database or the post table in our database okay so let's go ahead and do that hopefully that makes sense uh so we're using super here to access this function in django we're then going to use this uh attribute here to actually run so that's going to be passed over to here um to run the actual database and then we're just going to basically print it out so let's go ahead and refresh the page it's going to go back there and then you can see that when we look into our code here a bit further down now you can see that we're getting a query set we're returning a query set with two components because there's two items in the database so that just confirms the fact and is a small exercise of and the principles of what you're writing here and how it then is getting fed and utilized from the list view so just to reiterate that fact let's just remove this for now so it doesn't cloud our understanding we use list view and that worked absolutely fine didn't it we refreshed the page and it works so hopefully now you can see how these parameters these are properties these sorry attributes are being utilized by the list view class and other classes that are inherited in order to actually perform the action of generating this page here and displaying it on the page we're defining the post the actual model remember we're bringing the model in here and our model is referring to the model here this actual table in our database so hopefully you can kind of see how this now relates to what we're the classes that we're bringing in through that simple example so now we know those principles what we can actually do now is a real overwrite but this time of all the classes and maybe some of the methods that are within this class view and of inherited classes so what we can actually do now is set out a real overwrite so there's a method here potentially called get query set so we want to kind of overwrite that so instead of now returning the instead of returning all the data from the um posts model we're now going to actually overwrite that use overriding the get query set function so we're going to perform an override here so this is going to return something completely different so return and then obviously we want posts dot objects dot or in this case filter sorry so we're just going to filter out data that's in the database so in this case we're just going to have title uh start starts with and that's going to equal say i think i had a post here called a new post i think that's what i had uh and then we go ahead and further that filter filter that further of course um a little bit of slice in there so we're just going to return one item anyway so there we go so what we've done now is we've overridden this initial this original function called get query set and we've actually defined a different return for it so we're now returning not the initial the initial data that we defined here so we're just saying all the data from posts we're now overriding that by saying no actually we only want this data so when we go back and refresh the page does it every time when we do that and refresh the page it doesn't work so let's have a look to see when that is because i've forgotten that so let's go back and refresh the page and now we can just see the one item from the database so congratulations if you've done that for the first time that's your first kind of overwrite of the generic class based views now to fully understand that you would obviously want to now go into here into the django code and you can now start kind of playing around and having a look at all different classes so all this all these attributes could be potentially overridden for example but that's where you're feeding into if you want to make changes to the class based generic views in django so if that was a little bit confusing because i've jumped from super to this idea of just naming a function so let me just uh change this slightly so let's just use do the same thing but this time we're just going to use super so because we're using list view we're going to grab the get query set function now so let's just do that by saying query set equals super so we're going to use super this time uh and then we want to get the the method i keep calling it function i want to do that but on purpose because it's the same type of principles okay so method and function same thing um oh i say that loosely okay so filter and then um we want to run the filter so this time we want to do it differently so we're just going to return the same thing so what we've done here is we've grabbed the get query set now what is this is get query set so this isn't saying running the actual filter this is just saying get the query set so this function is based around the idea of getting this query set this query set is post and also objects so in actual fact we don't need to write post it or objects because we've returned that here by gathering this function this functions purpose it seems now is to actually get that information so it can then prepare the actual query on the database so qs if i just type in qs here in actual fact that's going to be translated to post dot objects and then i can run the query so i'm just summarize i'm using super to access other methods and attributes within the list view the generic list view and then i'm gathering on getting one of those functions or methods sorry the get query set i'm returning the data which we know is going to be post post.objects that's what this function does and then we're going to use that data to then finish off our query so now when i run this it's exactly the same so just refresh you can see it returns that individual post so there we go hopefully that brings you closer to the idea of class-based views and also to python stroke django object-orientated programming so we've been through some of the basic principles of object-oriented programming there was one thing i haven't mentioned which i was trying to fit into this but i felt as though that's probably worth its own tutorial and that's something called mix-ins so a mixin is basically almost like a normal class but essentially it's different because a class mixins are classes that essentially they're not designed to be used on their own they're developed uh to perform maybe an action which another class utilizes so let's just take this class um this animals class again i guess this is lightly speaking a um a class mixing because this class is not meant to be utilized on its own this has been this is going to be utilized by multiple classes right so very loosely this is essentially a mixing class because this has to be to perform to in order to get this to actually um contain any value we need to utilize this method or inherit this method into other classes so here i would obviously just inherit this in as per normal and then perform actions here so this is a general principles of what a mixin is so like i said i will be covering that in a separate tutorial and we'll go through that in a little bit more detail and create some different mixins but hopefully you've learned a lot from this tutorial if you didn't know anything about object-orientated programming or if you're wondering about how class-based views generally worked hopefully i've structured it in a way that makes sense to you if it doesn't i would love to know if you want to leave any comments in the comment section that would be great to give me an idea of whether this was useful or not
Info
Channel: Very Academy
Views: 7,111
Rating: undefined out of 5
Keywords: python oop, object oriented programming, django oop, django objects, django classes, class based views, django class based views, django view, django generic views, createview django, django view class, django cbv, django class based view, django, django tutorial, djangotutorial, django tut, django beginners, djangoproject, django examples, learn django, beginners django, django framework, django 2020, django example
Id: IBWiQMHcSQk
Channel Id: undefined
Length: 52min 31sec (3151 seconds)
Published: Thu Oct 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.