Django Mixins CRUD Operations in Rest Framework

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys welcome to the jungle rest framework tutorial in this video i'm going to talk about django rest framework mixins so first we need to understand the what are the mixins how to use it and what are the types of credit operations we can use it in the mixins okay so let me talk about first uh what is the mix sense generally the mixins concepts we use into the optical into the programming so where we can refer to the generally multiple inheritance concept okay so as we know that java and c sharp does not supports the multiple inheritance but it comes to the python the python supports the multiple inheritance by using the mrp principles we can use it and there are other languages also supports the multiple inheritance the point is very clear here when you are going to talk about the mixins sense we can say like this the mixins are a language concept that allows the programmer to inject some code into the class so in other words we can say the mixing is something like a programming style which is part of software development and in which units of functionality are created in the class then mixed in the other classes and also one more important thing is mixins are sometimes described as being included rather than narrated okay and also when i'm going to use this mixes definitely i use the classes rather than using the functions right because it is object-oriented programming this always supports the oops concepts right okay and moving to the next you can see on my slide there is some different uh languages are supporting the uh mixing this information i have taken from the wikipedia and here we can see that there is a court lane and pearl php python and ruby uh swift many languages which are supporting uh basically uh mixins of course these are all like so actually uh uses the object-oriented programming right so coming to the next one uh so what is the definition of the mixing so mix and definition is a class that contains the methods for use by other classes without having to be the parent of those other classes so assume that you have mixings into the uh like two different classes we can use those methods into the uh any class which you inherited uh and multiple classes we can inherit it at a time as well so that's where like multiple inaudible is coming into the picture right so make sense which is related to the optic 200 programming where we use the class no functions here so definitely the mixins we are using into the class-based views okay as simple as and coming to the point here and as we earlier seen that api view which is a decorator we are using at the function basic views and api view which is the class based view which we already spoken so far and now we are going to use something called gendrik api view and gender api view is a subclass of api view api is a super class apparent class and we are going to use the generic api view for the mixins okay so we'll see that um what are the classes we are going to inherited in the programming will get more idea about it if we're going to start the programming so i'm going to show you the uh walkthrough of for django rest framework website uh if you go to the api guide uh so under that there is something called genetic views you just click on that and it will take you to that uh respective page so you see that left side there is a list model mixing create model mix and retrieval update destroy this is nothing but a cred operations right you are listing the data and creating and update and destroy but there is something uh different is there uh respect to the list model mixing and retrieve model mixing so list model mixing will show the all the list of products which are there in the our table so in our case we have all the mobiles all mobile's information will show in the list model mixing but if it comes to the retrieval model mixing only specific model information can be shown so this is nothing but detailed information list model is all list of products and the restaurant is same if you want to create uh any product and create model mixing if you want to update the product information we can use update model mixer and destroy is nothing but delete and i'm going to take you something about api reference at the generic api view which is a generic api which is very important okay so so what is the use of this generic api view so you see here this class is extended the rest frameworks api view so in previous sessions we have seen that inheriting the api view now we are going to use the generic api view going forward and this generic api view is the subclass of api view and what we can do right so basically we use this uh generic api view with any one of the mixing classes so this is how so first we need to understand the generic kpi view then we have to go to the mixings so very simple when you are going to do any crud operations on the jungle jungle rest framework so you have to go and use the gender kpi you plus any one of the mix in which you can use it here right so the same thing which we are going to implement here for example if i go and click on this list model mixin and you see that when you are going to implement the list model mixing first you inherit this class then you write list overridden method simple way like create model mixing again over it in this and retrievable model mixing you can override in the retrieve method so every class when you are going to inherit especially this mixing classes we need to overwrite this uh respect to uh methods okay so update method also we need to so for destroy it is a destroy only so i'm going to do the same thing now in programming you'll get more idea about this so in what is i'm going to say that here when you are going to implement the mixins we are going to use the combination with generic apiv and we'll see that all pros and cons and also how we can face the different kind of issues when you're removing something and when you are not worried in the method what kind of issues we are going to see it in shortly i started the server and you can see on my screen so already i've opened this urls and also i'm showing now django administration so to later we are going to update these products update or delete whatever actions we do all credit operations which we are going to perform now so to begin with this code uh so first we need to go and create the view and that view must be called from the urls so i am just directly going to now views so just click on the views so in the previously uh we have done something like okay so in previous so we already done uh this uh like detailed view and list view based on the api view class now i'm going to use the generics so as part of generics already i imported the these two rest framework import maxims you have to import first from rest framework import mixins from rest framework import generics so what i'm going to do right i'm going to take my own class i'm going to create my own class class name is your like say that list products mix in so to to make you more understanding here i'm giving the direct names but you can change it also so list products and mixings okay but this is just for understanding purpose i am doing it so once you started uh going with this way generally after that so list product mixes after that what we can do we have to go and add the mixins here so what i will do right okay simple way and mix ins dot and the list i want to display the all the list of products right so list products and i have to use something like generics dot and this is a generic api so this is where like we can call it as the uh multiple inheritance you see that there is a two classes we are using one is from the mixings another one is the generics so you are like implementing the the behavior of it here and i am going to do something like i am going to overwrite the get method that's what right we have seen the uh in the documentation so when you're using the something called list model mixing we need to use the get method and of course here the first parameter is the cell as it is a like instance method and the next parameter is the request okay and uh after that we use the arcgis that is the variable length arguments are like keyword arguments uh which we can use so k w a r g s okay so s so after that uh what we'll do right we are going to written the data to send the data back to the once user is requested that is the request object we take and we need to send the request back right so we need to written basically so self dot and what we can use we can say that retrieve or retrieve method or get method so let me use the get method here we'll see that how things goes now so this get me the first parameter is always request and second parameter is like star args and star star kw args right simple this is how i've written now i'm going to call this list product mixins and remember here i am not included something like query set and serialize class i will see that what kind of exceptions we are going to face then after that i will write a query here model query suite without using the model for how the data can come here i'm not use the model query right so we'll see that what kind of exceptions we are going to get it now and the urls uh i'm going to call this let me copy this these products mix-ins i'm going to now in this i'm just going to add the path so path of and we can now right here this is a mixing path so mix in path and okay so just for understanding i am giving this patterning but you can change it later also now the views dot and we can use the class name dot as view okay so next name is as if no name is not make big difference i'm just going to add mp mp stands for mixing path you can change it over the name okay so i'm done with this adding the paths now i'm straighter we go to this browser and i'm just going to enter here see that there is a new path which is got added what i'm going to do right i'm going to give this path and just you enter so i'm just going to enter and it's giving something like page not found so we'll see now what is this page not found is it the mixing path i've given and did not match any one of this so it is okay so i'm going back to check uh mixing path okay it's because of i'm not given the forward slash okay that's my bad i'll go back again i'm going to enter now you see that there'll be an exception here so maximum request depth is exceeded okay so what i'm gonna do right okay and we'll see now there is uh two things which we are going to focus now so instead of giving this uh self dot kit what i'm gonna do right i'm gonna change it to the list also so actually basically it is a list method but i have given the get now i will go back to the again of microsoft edge which is a browser name and you see that this is the correct way of implementation so what is the mistake which i have done previously it's not mistaken intentionally i want to make you understand here when you are using the get method you need to use the list method okay to written it so you just three combinations here list model mix in and get method and you are written in the less data this is the combination you need to go and when you see the exception here we are not getting the saying that query set is not written so i am going to add the query set here spoiler reset is not a big deal but remember that you have to use the same name so here instead of query set if you use your name or like any customizer name it won't work okay so we have to use the exact query set only so product dot objects dot and all is the function so i'm going to use it here and what i'm going to do right i'm going to use the serializer class so serializer class which is the predefined what i can say the variable name we have to give the same name and we need to point to the product serializer as simple as so in previously you've seen that many things we are written we are sending the object and data and everything previously in the sense when you use the api view but when it comes to the mix sense we are simply query set serialize class and get and get means we are getting all the products here that list we are written in here so what we are going to do right i am going to check now the data which is showing or not when i go and refresh the screen so let it take time so refreshing it now you see that all the list of products which is showing now and you see that list products mixed in so whatever the function name you queue so your function name sorry whatever the class name you give same class name which is like list products and mixing same name is coming here with spaces because there is a different words are there list products and mixes okay now this is something like retrieval okay i mean it's not retrieval it's a kind of retrieving the data and the credit operation is nothing but read the data so we are talking about r now let's go to the other types of uh operations which we can perform now so now let me go and write a different class for the detailed view so i can say that detailed so it's a class right generalized starts with of the case letters as for naming conventions so detailed uh products uh mixins okay our detail product right not products here it's a product so detailed product uh mixins and i'm just giving the name here the mixins dot so here i will do the different operations example uh get one product information post one product information and update one product information delete one product so deleting one product updating one product like that okay so basically we are specifically doing the one product here and this is nothing but list all the product information we are trying to retrieve here that's the differences now the mix since dot i am going to write uh everything here right so for example i want to write retrieve modal mixing so retrieve model mixing is especially for one model okay i mean to say that one product information basically right so i'm going to use the generics here so as is we already use the generics dot uh generic api same way so generics dot gender kpi so we can write a number of classes as i said mixin's concepts is going to help in python it supports the multiple inner text if you want to understand the object oriented programming already i created one video uh so i will give you that video in the video description and you can you can check out that so now going to this the mixins dot and the different classes we need to provide for example i want to update the content update model mixing if i want to do something like uh create if you want to create create model mixing if i want to destroy and i can use the mixins dot um so simple dot destroying right these are the uh various so current operations we can do this is for retrieving one product update one product create one product destroy one product so that that is the combinations basically we are using so it's a class we have to go and write now we are going to use the same query set so let it be so quite respect is same and serialized class is also same so there is nothing is going to change in this case but the only thing is that okay so previously the list model mixing we use the get and list and at the same time now retrieval model mixing so what we are going to do right so it is a get only so i'm just going to copy the same thing why we need to spend time on this right so df it's a function it is instance method and get self request star arcs and kw arches it's nothing but keyword arguments and after that we can use the same written statement also here so give the written statement instead of list we are going to say that retrieve of course when you type your ultimately you can see that review okay and we are done with the get so let me go and call this method and after that call this class and after that we can now use the different grad operations in the same class okay so i'm going to now the urls because since we are talking about very specific here so i'm going to use the path and inside the path we can use that so detailed um mixing are detailed mixing so we can say are like one product product mix mix in right so we can give any name this is just url path only um so that's not a big deal just give the path here and after that views and views dot give this detailed product mixins dot as view right so we are calling the class based view so that's the reason and name is equal to your mdp so so model detail are like we can say mixing detail product any name is fine because we are not using the name right so absolutely fine so product mixing and this is how we can now define the text so what i'm going to do right i want to access this so first of all let me go to the uh browser here so i don't want to be in the mixing path let me go and enter first sorry i can go just enter there is a product mixing and mixing path so i am going to enter your product mixing just see and there is a correct view little product mixing to be called with url of course right you can see there is a because we are we are dealing with one specific product so you need to pass the argument correctly so after this what we can do right so we are going to use url passing the uh it's nothing but url dispatcher we are going to use the um some id we need to pass okay or like some column we need to pass so in this case by default it's a primary key which is an integer in colon and pk okay so i think this is done now give the forward slash so we'll see now it whether it works or not and before that i want to list out the values here so before going to take this url and i want to show you that okay just enter there is something called mix in path so this mixing path which i'm going to copy so i will show you something then after that we'll go to now see that everywhere there is id this id only we need to provide after the path okay this id is nothing but primary key pk is nothing but primary key so id is the primary key in this case so i'm just going to give here for example um anything we can take for example iphone charger 5 so what i will do right i'm just going to enter here and use the product mixing after that product id so nothing but uh 5 so i'm just going to enter here you see that respective product id but only you can see the get right but you are not seeing anything like put our post so i want to first update this i will go to the visual studio code and in our views i'm going to add another one so what i will do right i'll add one more row function here so it's nothing but the method we can call it as in the optimum programming in the classes so here in stock get i'm going to use the put because this is update statement and return and of course it's not retrieved here it is update so i'm just going to update restaurant is same only okay same query set same serializer class but only thing is changing here is calling the method right so we are overreading the method this is it's completely inheritance and polymorphism concepts guys okay so i'm just going to the again now your browser and refresh you see in the below there is a put okay which we can see it uh if you want to update okay instead of charger i can say that iphone 11 no i can say a phone case so you can write your phone case something like this i just want to update this is not the charger uh okay we can put it as a headphones iphone 11 headphones right i'm just adding some text here just going to add this click on this port and you can see this iphone 11 headphones is added and more important thing is if you want to see the raw data we can see in the json format if you want to see into the html form also we can see so put is that just updating the data for example i want to create something just you can go and use the same statement same and same code which i'm copying you and changing the method name so i want to post post is nothing but creating so update uh which will change it to the create now so everything is same so i will go back to that again now so screen and before that you need to refresh it otherwise it won't work and i'm just refreshing here and you see that there is a post also here for example if i want to post something id we can post for example 900 and the name here or 45 sorry name some name here the macbook so i'm just referring to your cost is 200 or 3 000 whatever it is so we can give the date here so is there any way like you can give any date just drop and came and you give this this is a test mac and in stock mobile phone okay there is no okay i want to create the product here if you want to create a product you can create product category we have the product categories here i'm going to create like a laptop so let me create here and it's the categories okay we can say 10 and save this that's not a big deal so i don't think so it will come by default we have to refresh the screen and no problem we'll refresh it here and write 9000 and name is the mac book pro and the cost is like 2 000 or something example and description is like a test and the date here something like 26 and mobile phone instead of mobile phone uh i will use the laptop now it is showing now so just click on the post so you see that there is id 15 and 900 micro pro which is created if you want let's do a clock if you want to change the data you can change it it's like so that's it if you wanted now you can see that is created if you want to change the data you can change it so something like that it works very well right so and this is how the detail we are seeing the cat we are seeing the post we are seeing the port and last one is the delete our desktop so i'm just going to copy this entire code here and below and we'll see now how it works so instead of post what i'm gonna do right i'm going to use the delete here so delete uh we'll see that whether it works okay delete yeah and install create and you say that destroy so as simple as i'm just increasingly so destroyed and now this is a perfect now right so what i'm going to do right i'm going to the again url and so you can refresh the same data is entered here and that's fine you can see there is a delete button if you want to delete here you just say that as you want to delete it will detect so something like that you need to be very uh be careful now how you when you are deleting the data so we are seeing all the crowd operations here listing all the data deleting one product and getting one product information posting one putting one data everything which we are able to see it in the uh like with respect to the uh this mix sense so previously we use the uh like a lot of get and put a lot of code we have written but it comes to the mix since it is easy your job is easy don't need to write too much of code and you just inherit and you do it but the only thing is you have to remember in the urls we need to pass the specific primary key then only that value will be deleted hope you understand these concepts if anybody having any questions please write your questions in the comment box i will try to help you and one more important thing is that in the next session i will come up with the generics api view cred operations and after the sessions i will talk about the differences between the both generic views and this mixings will get more clarity okay hope you are liking this content let me know if you have any questions or something i would really happy to help you thank you [Music]
Info
Channel: IamPython
Views: 398
Rating: 5 out of 5
Keywords:
Id: sx8BVOrWN-8
Channel Id: undefined
Length: 26min 44sec (1604 seconds)
Published: Sat Mar 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.