Build And Deploy a REST API with Django REST Framework #4 Customizing the Django Admin [Order Model]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone and welcome to this video in the last video we created our custom user module now went and said and also registered our model so that we can access on it on the admin panel or our admin dashboard so in this video we're going to look at how to create our order model as well as to customize our admin model so what i'm going to do is to go to our order application so i'm going to go to all this and within modus dot pi we have iterate our order application now i'm going to open up my documentation for the orders we created so i created them using io i'm going to open the diagram which is this so just like you can see here we created a simple documentation of our of how we want our model to be they're going to be implementing the order table so i'm going to do is to create our model so i'm going to create a class and this is going to be a class of order so this class is going to inherit from models dot model and the first thing we're going to do is to put the into consideration the relationship between the user as well as the order table so one thing we can see here is that we have a one-to-many relationship between the users and the orders so that means a user can have very many orders so what you're going to do is to import our user model so jungle gives us that gives us a very important a function called the gate user model that can help us to access our user model even without having to call that same user model so to do that we are going to import that get user model so i'm going to do is to say from django dot contrib dot auth so we're going to go dot contribute authorize import the gate user model and after i put it i get user model they will create a user class using the get user model so right here i'll say that our user is going to be equal to get so you're just going to pull that get user model function so i'm going to say get user mode and right after doing that then you're going to first create a field for a customer so customer is going to be that person is going to create um an order so we shall say that our customer is going to be equal to uh models dot foreign key so in this case uh this is going to be foreign key the first thing we have to specify is the model to which this is a foreign key to so this is going to be a foreign key to the user model and then you have to specify that when we delete what happens so when we delete an order or when we delete our user we expect that the orders attached to that specific user are also deleted so to specify that we are going to say on on delete and this is going to be go to models dot cascade so right after doing that you're going to also write other fields so the first thing we're going to do is to specify our order sizes so i'm going to come and say call this field our size this is going to be a models not character field so this is going to be more does the character field which is the same thing as a string field then we're going to specify the max length so in this case we're going to have our max length so i'll set our max length as 20 so basically give it 20 and then i'll say that this is not going to be now so this by default this is not going to be now and it's also going to be choices so right here i have the different choices you have small medium large and extra large so the way young behind those choices is that we create a tupo uh that tuple has the different keys and the different values for whatever uh size we may have so i'm going to come right here and i'm going to create the tuple i'm going to call it sizes and then this size is going to be a triple so this support is going to contain the varia sizes of our pizza so the first thing we're going to do is to add that small so i'm going to give a key or small and then print access more by by setting the value as small so i'm going to do the same thing for medium i'm going to do the same thing for large so this is going to be large and i'm going to do the same thing for extra light so this is going to be extra extra large and right after doing this then i'll come and specify that our choices so this is going to be choices so our choices is going to be equal to sizes and right after doing that i'll go ahead and do the same thing for our other statuses now here have penny in transit and delivered so i'll come right here and specify so i'll come and say that our order status is so i'm going to call this order statuses and actually let me just call it order status so right here the first thing we're going to have is pending so we shall have pending and then also going to have uh this is going to be uh in transit so we're going to call this in transit this is going to be in transit then we're also going to have a delivered so i mean set this to deliver now after doing that they are going to do the same thing so i'll come and say order status is going to be called models dot character field we can specify our max length as 20 then also give the choices as our order status and after doing this we're going to also specify our quantity so i'm just going to come right here and say quantity is going to be equal to models dot integer integer field now one thing we forgot here to do is to specify the default for these uh choices so i'm going to do is to come and provide a default name in this case we expect the default to be the small size so i'll say default is going to be equal to so we're going to access the first choice which is going to be as small so the way we access uh values within tuples is similar to how we access them in lists so we use indices so what i'm going to do is to come and say this is going to be sizes and then you shall have to access the first the first size and then access the value of that first size so i'll do the same thing for other statuses so i'll come right here and what i'll do is to say that the default so i'm just going to minimize this so that we can be able to see so again set that default as in this case our default is going to be order status 0 and then we should also set this to 0 meaning that our first order status is going to be pending so by default we expect our order status to be pending so i'm going to do is to also create this created ad so pretty that's just going to be a daytime field so i'll say created sorry for this so it's going to be created at and this is going to be modus modus dot date so this will be mode does the date time field and then what i'll specify is auto now at equals true so this means that every time we create an order it's going to track the time at which you created that order so also say updated that so this is going to keep track of wherever we shall update and all that so it's going to be models the daytime field and then our specify auto now equal to true now the difference between auto now add and auto now is auto now i just called every time we create an object while auto now is created is called every time we update an object so i'll go ahead and also create a function that's basically going to define how we get string representations of our model object so i'll say def so it's going to be str and shall it say self then simply just going to return an f string so this f string is going to contain uh the information about an order so shall say in this case what you want to return is going to be order so in this case we just want to return it they say so i'll say oh that says ah okay so in this case what i'll do is to say uh so i'll say self dot customer dot id so after doing this i'm going to save and the first thing we're going to do is to create a database migration for this so i'll stop a server and say python three monitor p right and then what i'm going to do is to create a migration so in this case i'm going to say create make migrations sorry for that and then i'll specify the app for which we're going to make this migration so this is going to be our orders app so this is going to create our migration and the next thing i'm going to do is to write this migration to our database so i'll come and say python3 magnitude py and then migrate so this is going to go ahead and write our order model to our database the next thing we're going to do is to register our model with our admin interface so i'm going to come and so let me first close this and after closing this i'm going to head over to our files and then i'll go to admin.pi within our orders app so what i'm going to do is to come right here and the first thing we're going to do is to register our admin dashboard now for the previous video went ahead and registered our user model in the admin but he did not carry out uh different customizations on how we are going to use our admin but the good thing about admin panel within django is that it's very nice that you can customize it to our needs so in this video you're going to look at how we can customize our order model on our admin so let's go ahead and do that so what i'm going to do is to import our order modulus ourself from dot modules i'm going to import our order sorry for that so it's going to be our order and after doing that then we're going to go ahead and we just say to that admin so the first thing i'm going to do is to call that at admin.register so we need to decorate our class of our order admin and then we go ahead and specify the different customizations so the first thing i'm going to do is to say at admin dot register and then i'm going to specify the model that you're going to register so in this case i'm going to call our order model and after doing that then what i'm going to do is to write our class so in this case we're going to create our class i'm going to call it our order admin so every time we want to create a custom admin then you're going to create a class that's going to inherit from admin dot mordo model admin and as long as we have uh decorated that class with admin.register then we can go ahead and create the different customizations for a specific object so i'll do is to come right here and the first thing i'm going to specify is that this display so the list display is the list of fields that you may want to show on our admin interface so if i say that the fields that you want to display so i'll go to modules.pi and let's say i want to display the size the order status and the quantity so i'll come right here and say one to display the size uh the order status so i'll come and say order status and the quantity so let's say we also want to uh display the actually let's first uh display this so when i accept this uh we train ourselves again with python many dot pi run server so when we head over to our chrome right here when i refresh we are going to have our orders as well added to our admin section so when i go to our orders i'm going to create a new order so i'll come and create a new order so i'll come specify the customers the admin user so by default our sizes are small and by default our other statuses are pending so i'll say that us our sizes is small and uh in this case our audacity is going to be let's say pending so it's let's say we specify the quantity as true so when i save this right now we can see that the fields we specified the size the order status and the quantity are the ones that are displayed on our admin dashboard so this is good as we can go ahead and customize so let's add one field so let's say we want to also specify the date we created so i can say created at and save so when i save this a server is running so when i refresh we can see that our created that date has also been added so let's go ahead and add another customization so in this case you may also want to have fields that you may use to filter for example it may filter by order size filter by the order status so to do that we're going to use our list filter so this filter is also a field of those things that we may want to use to filter our order so for example we may say created at as well as let's say we may also say when we want filter by order status and let's say we want to also filter by our size so let's say i want to get the size of that pizza so in a safe and refresh in this case we see that our list filter has been created so we have filter by created add filter by other status as well as filter by the different sizes we have so let's go ahead and create some order so when i add some more order so the customer is going to be the admin since we don't have any other users so let's say we have the size as medium let's say we have delivered so let's say the quantity is 12. if i save this then let's say i add another one so if i say that user is that let's say in this case it's large and let's say it's in transit so let's say the quantity is 34. so in a safe now we have different uh orders different orders here so we can go ahead and filter them by let's say the date created so let's say filtered by those that were created today so this will return only all of them since they are created today so let's say if we filter by the past seven days so this basically won't work because there are no ones that we created by the last seven days so let's say i wanted to filter by pending so if i went and said pending will only get the pending order so let's say we wanted to get that one in transit then you return that one in transit if i say they leave that so this is going to return the one without that status of delivered so let's say i want to filter by let's say the size so i can filter by medium and this is going to return the medium so if i say small then since we don't have any small size it's going to return zero orders so if we say let's say we want to specify large and extra large so it basically does the work as you want it so it can also as well say clear all filters and that will go ahead and clear all filters so in this video we've been able to create our other model as well as to customize our admin dashboard so the first one thing that we forgot in the previous video is to go ahead and specify our object so i'll get to our authentication uh app and go to modules so we created our custom user manager as well as our user module one thing that we forgot is to specify how our user objects are going to be created so to do that we just call objects and then we say that that is going to be equal to our custom user manager so i'm going to come and say it's going to be custom user manager so right after doing that then we can successfully create create our users using our custom user manager so in this video we've been able to uh create our other model as well as to customize our orders our orders on the admin dashboard thank you for watching and if you like this video please leave a like do not forget to comment on what you feel about this video uh you help me by subscribing to this channel thank you for watching and see you in the next video bye
Info
Channel: Ssali Jonathan
Views: 20
Rating: 0 out of 5
Keywords: django admin, django, django admin tutorial, django site admin, django admin panel, django admin page, django admin tools, django admin interface, django admin login, django admin register, create admin django, django model admin, django custom admin, django admin customization, django tutorial, django administration, learn django, django 3, django modeladmin, django tut, django beginners, beginners django, django examples, django suit, django admin dashboard
Id: WLV7BJrFvxU
Channel Id: undefined
Length: 18min 32sec (1112 seconds)
Published: Wed Oct 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.