Django tutorial - How to improve your Django admin

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to a new tutorial in this one we're going to be working with the Django admin and before we get started I just want to make it very clear as to what we're going to be going through in this tutorial what we're not going to be doing is showing you how you can rebuild the entire admin to be some fancy panel that has graphs and tables and things like that when we say customizing the admin we mean customizing the built-in admin that is really provided to you by using features on the model admin class that's provided by Django if you're interested in something like a complete makeover of the default admin site then leave a comment down below because that is something pretty interesting but what we are going to be going through is showing you how you can add new features to your model admin which is the most important class when working with the Django admin and so I'm mainly going to be taking a look at that and showing you how you can use those new features to enrich your experience when working with the default Django admin so the project that I'm working out of has already been built it's the video membership site which you can get here on get up if you just go to our video membership repository then here you can just clone or download it and that's what I'll be working out of we're not going to be touching any of the models or anything like that we're purely just going to look at the admin and the reason for using this project is because they are already a few models that have been set up there have some good relationships that will allow us to explore a lot of the documentation and so before we get started I just wanted to bring up this image which is a screenshot of the poll that was posted a few days ago and currently the option of the highest vote is the school system with multiple users but I just want to point out that we are going to add all of these onto the channels so it was just to get an idea of if these would be supported or not but it seems like they all are and that's really cool so we will add all of them eventually currently we're working on the school system with multiple users and that'll be up as soon as possible whereas the remake of the Django ecommerce I'd rather spend a little bit more time on that to make sure that that's a really good project so most likely that will not be up within the next month or so whereas the business and analysis tool could also follow shortly after the school system and so that's just a heads up on the poll so thanks to everybody who's contributed to it and with that being said then once you've got everything set up in V s code or whichever editor you're working with then we can get started [Music] okay so here in the project I've got the server running and we can take a look at this right here I've got the admin open and we still have everything from last time which were these memberships and the subscriptions and the user memberships and if we take a look inside our courses app then if we go to the models we've got a course model and we've got the lesson model and if we go to the admin then we have let's get rid of that comment we've got the course and the lesson imported and we registering them here inside admin dot pi now when you create an app by default Django searches your app for the admin dot PI file to look for all these things that need to be registered and this command admin dot sighted register will register that model now those are the basics that's what everybody more or less knows and that's why we can see them here in the admin now if we go to the courses and what I'll actually do is just open this in another tab and if you go to lessons here then let's click on the add course button there and we get this form to fill in and if we could add lesson we've got the add lesson here now if you're happy with the setup then keeping it as it is right now would be fine however you might be interested to see how you can actually combine these two into one form so that whenever you want to add a course you can just add its lessons right there and then you don't need to go and add the lesson here and then save and then reload here and add it there and that's something that's very convenient and the principle of it even though it doesn't look that great because it is that Advan the principle of it should inspire you to create forms like that well you don't have to go and save something in one form and it go back to the other one to use what you just saved and so we're going to eventually work towards displaying the form in that kind of manner one form that can create the course with its lessons and it all starts with the model admin now the model admin inherits from this admin import that we already have there and what I'm going to do is actually just comment those two out and what I'm going to just create a class and we'll call this the course admin and it inherits from admin dot model admin and for now I'm just going to say pause and then to register your model which in this case is the course you can then call your admin dot site or register but then pass in the model admin that corresponds to that model so we would pass in course admin like that and the service still running so if we come back here I can just go back and you can see that it's the same now inside this model admin there are a lot of attributes that you can specify to change the way that this looks and actually change it quite a lot one other thing to note is that you can also register your models not only using this command but you can also use a decorator and so you can simply just call at admin to make it a decorator and then say dot register and pass in your model of course and you can come back here refresh it and it's the same so those are just two ways of doing it you can use the decorator or you can use the register command I personally think the register command is better so we'll just leave it like that now instead of calling pass here we can get started with displaying something and the first thing we're going to look at are the fields so you can see that this is one of the prompted attributes and this is a tuple that takes in the names of the fields on the model so for example on the course which is what we're working on you've got slug title description and allowed memberships so we could then take those we'll just copy them and we can bring them into the fields here and so now you have some fields and then we can just remember to register this as well and we have courses then if we wanted to add a course you can see all of the fields are included there whereas if I were to let's say remove the title and we come back here now you can see the titles gone so these fields relate to the add form whenever you want to add a new instance of whatever model you're working with then these fields define those fields for the form and you can also override the form itself if you just simply just create a form sup I create a form normally then you can come in here and specify the add form and the normal form we're not going to look at that because there's nothing we want to override but that's just to let you know about that so in our case we want all of those fields to be there when we create the course another way that you can do this is by using the field sets now the field sets is a list of two tuples and this is also again to customize the add form and the change form now each tuple inside our list has a name and in the field options so for example we can pass in none as the name and then the option do you specify in a dictionary and this would have fields and then be everything that we just showed right there and we can comment that out and we can just add a comment at the end there and if we come back we can refresh this and if you click Add course then you've got all those fields there so these fields and field sets relate to when you want to create or change instances of the model but now let's look at something which is just when you list out all of the instances and this is the list display attribute and so the list display is basically a list of fields that you want to show when you list them out so this here is the list display and you can see there right now we only have this Django introduction being displayed to us and that is the string method on the class that's being displayed so if we go to the course you can see define string returns self dot title and that's the title field on the model so Django introduction is the title of this course you can see that it's the title so that's why it's displaying that here in the list display but you can change that here inside the model admin and we could for example show the slug and show the description now if we save that and come back now you can see that we get the slug and we get the description however you can't add the allowed memberships in there because this is a many-to-many relationship you can see it here on the model means many and you can't put that in a list display so we'll add the title back in there and just take one more look and then you've got the title slug and description and already this provides you with a little bit more information just the fact that you can see the slug and the description is a lot nicer than only being able to see the title and you can create your own methods on the model admin class and reference those in the list displays so for example we could say define combined title and slug and also it takes himself and an object then we'll just say return and we'll just dot format this to have the object title and object slug and then you can take this method and actually bring them inside the list display and you put it inside quotes as well and if we refresh this now you can see there's the combined title and slug and it's giving you the title - slug so that's quite convenient depending on what you actually want to display in there now the next thing we're going to look at is similar to the list display and this is the list display links and this is basically the same as the list display except these items will become links so for example if we have title and it's a slug as links then if we come back in here and refresh this now you can see that I can click on the slug as well and if I do click on it and it takes us to that instance and that's what was already happening by default with the title there we click on it and it takes us to that instance and you can do the same thing for the description and the combined title you can just add them in there and now that attribute becomes a link and you can click on it something else relating to the list display is whether or not those items are editable and it's actually just close out there so we could say list editable and this is again a list of fields that we can edit straight away here on this list display so for example let's say the slug is editable and just make sure we add a comma and I see we get this cannot be in both lists editable and list display links and that does make sense so let's get rid of it from there and if we come here and we can see that the slug is now editable you can actually change the slug right in here instead of going to the instance and changing it there so let's change it let's make it like 104 if I select it I click Save now it's changed and you can see on the slug it's also changed here and the combined title and slug so that can be quite convenient if you have some field such as a slug there you just want to edit quickly the next one is the list filter and this is a list of fields that you would like to filter by so let's say we want to filter by the title and the slug then here if we refresh this now you get this little filter here on the side and it says filter by Django introduction filter by Django 104 because those are the only slugs and titles we have available right now and it doesn't make sense to filter by a title or slug this way because if you had a hundred titles then you'd have to look here to actually find the title that you're looking for and then click on it so that doesn't make too much sense but if you had for example only three types of products that you sell maybe you have 100 products that are listed here that were part of the orders but they are only categorized into three types of products so maybe like a chair table and a lamp then you could just come over here and filter by those specific items to see the result here in our case where if we had let's say a hundred different courses and we wanted to filter them by the name it would make more sense to have a search display and that's what we're going to look at now we're going to look at how to add a search field and this is simply just with the search field attribute and this as well is a list of fields you want to search by so for example title and slug and if we come back here and refresh this now you get a search bar and we could search for Jango and we get that only instance there that's our search for react then we get no instances so it is searching and the search field search is in a number of ways such as checking if what you searched for starts with a certain thing or ID contains or I exact those are some of the ways that it finds your matching values and there's a lot more that you can do with search results so I do encourage you to look at the documentation which I'll link in the video for you to check out because there's a lot on searching and so now we can finally take a look at how to display our lesson on the course and this uses another class that comes from the admin which is the inline model admin so we can go ahead up top here and say class inline lesson and we'll say that it inherits from admin dot and here if you just type in then you'll see that you get two types you get stacked inline and tabular inline and basically these two just differ in how they display it either vertically or horizontally so let's add stacked inline and see what that looks like and we'll just specify here that our model is the lesson and then we can take this inline class and in our course admin we can add a field here which is the in lines and this here is a list of other classes that are inheriting an inline based class so I can just add the inline lesson and now if we come back here and refresh this let's click on adding a course and now you can see that we get our course here but we also get the lessons and it seems like we were given three lesson forms because you can see here we've got lesson 1 lesson 2 lesson 3 and they're all displaying on the same form as when we add a course and you can also see that they're stacked on top of one another as well and you can click it to add another lesson and that's lesson for lesson 5 and then just come here to delete those but we can't delete these original three now to customize that a little bit you can add the extra field and this specifies how many extra forms you're going to display so for example if I set it to 1 and we come back here now you can see that we only have one form instead of 3 and let's take a look at what the tabular in line would look like so we just change this to tabular and if we refresh this now you can see that it looks more like a table where the fields are next to each other in the form whereas with the stacked the fields were stacked on top of each other and in some way this does look a little bit better because the fields that we're creating don't require a lot of vertical space they're just a slug title a position a video URL so in this format it actually looks pretty good and there's a lot of fields that you can also specify on so for example you could specify the max number and this is the maximum number of extra fields that you could display so for example 3 so if you refresh this and let's say we add another this and add another lesson now the actual button is gone we can't add another form and so this brings us to actually the end goal that we wanted to achieve where we get the forms on the same page and in this way it allows us to create the course much more conveniently so if I want to create a course now I can simply just pass in all the values to these fields click Save and then you have a course with its lesson already added compared to how we originally had it where you had to create lesson and then create the course separately which was just a lot more effort so if you enjoyed this video leave a comment down below let us know what you thought otherwise thanks for watching don't forget to subscribe and we'll see you in the next one [Music]
Info
Channel: JustDjango
Views: 45,600
Rating: undefined out of 5
Keywords: django, django tutorial, python django, python, Django Web Framework (Software), Python, python programming, python 3, python 3.5, django python, python tutorial, tutorial, how to python django, install django with virtualenv, django 2.0 tutorial, django admin, customise django admin, improve django admin, django admin tutorial, how to use django admin, django admin basics, the django admin, django admin introduction, customize django admin, custom django templates
Id: rxai34qeBcc
Channel Id: undefined
Length: 18min 20sec (1100 seconds)
Published: Mon Dec 10 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.