Customizing the Django Admin: The How and the Why

look on everybody we have today customizing the Django admin roschmann Prasad here is gonna tell us all about it there’s a lot of people here that knows a little about Django some hands oh cool awesome so well we learn everything about how to customize the Django admin is really sweet so please welcome thank you for the introduction hello everyone my name is Lakshman Prasad has directly into deals that work for McKinsey and company as an architect so I have use Django admins have been involved the Django in the Python community since the 1.0 days I really like the Django admin and we’re trying to see what I’m not really going to cover too much of the technical aspects although yes we are going to look into the technical aspect which would much more about you can always get the technical aspects about how to customize on the documentation but what you don’t often get is why you would want to do such a thing I will try to see how I can try to make justice to do it speaking about today you might know me from my twitter ID or from a lady said idyllic when I used to actively blog about together when they should know about me at all otherwise so the Django admin at the django documentation even before it says what that man is gonna suggest it is one of the most powerful parts of Django do you guys agree how many people actually Django admin is one of the strongest parts lehnga I see how many people agree that at some point it used to be at least one of the strongest most powerful parts is anger I see exactly I know I the feelings about trying to suffered a lot in the recent popular perception today in I didn’t elaborate his talk at Google in 2008 on the Django Cohen well he has the question how many people came to Django because of Python the answer was an overwhelming 50% of the audience or more than that the question that wasn’t asked is how many people came to Django itself because of the Django admin I don’t know the answer but I’m willing to guess to make a guess that it would be much higher than the number of people came to Python because it’s angular in those days hardly ever a discussion took place about Django they did not mention the Django admin in one of the discussions about the future of our future of Django and when in the mailing list there are quite a few of them in one of them Jeff Croft rightly pointed out that nothing from the web from five years ago is still dosimetry standards so since in 2000 the year 2008 when Jenga was such an important Django admin Oh such a most powerful part which brought a lot of people into using Django and thereby using Python itself how does it fail but fatally to the standards of 2013 so what all does younger get wrong whatever the what is the popular perception of what are the things that Django admin is lacking in a slightly pointed by somebody on the mailing list it doesn’t get the padding right you can you see where the levels of the heading in the top header as well as the bottom header the front the padding is is not exactly the same – in the top header as well as in the subsequent headers it’s a powerful tool it has authorization filtering authentication and all forms of customization can do but it should not use it for external application isn’t that the common prevalent wisdom admin is not to be used for is only to be used by trusted internal users there are charges that Django admin is really hard to customize I like to say there are plenty of articles that describe to you how to change the models what model admin parameters to override and then what templates to customize and where to put them but when you actually get to the point of how do i implement this functionality in it when there is really not that much resource you know for you to go and find out unless you know what terms to search for on Stack Overflow because everything is here so anything with the word admin in it unfortunately is going to be compiled to the WordPress admin it’s not even the same thing jiang Birdman isn’t an application like wordpress admin is it jang geurim is much more like a tool in order to wear such kinds of admin in fact something even more but that doesn’t stop people from comparing Django admin and saying it’s user experience is not nearly as good as WordPress admin yeah other than the minor revolutionary in Hatton announcement Sango has received Django admin has received it has been more or less the same ever since the day they appeared more than half a decade ago the last major change was the new form said

when when 1.0 was about to come out since then – till today there’s only minor revolutionary changes there hardly anything part breaking right now it doesn’t get enough loud this is also a charge that many of the other accountable occasions receive should there be a contribute all that’s another beast but I don’t want to get on into that discussion that’s how it’s go for current discussion so I see people are thinking wasn’t this supposed to be talked about the good chicken dualism Django admin wise as they are talking about all the things the anger diamond is lacking to be clear I was only saying those are all the popular perceptions about Django admin with the necessary screenshot evidence where possible I’m not saying all of them are true in fact I am going to contest despite all of these even though some of them are true Django admin is still a fine tool that enables the dial-up dial-up or developer I’m very good functional application in the true spirit of Django it’s a great tool for achieving the stated objective Django what is the stated objective jenga you’ve all read it on the website to enable perfectionist developers with them to deliver and deadlines right so some of these things that whatever charges we saw there is relevant some of them can be changed easily and some of them can you can change easily and then of course it can get a little more loud so we just went through some of the common perceived issues of admin over the course of the rest twenty minutes or so I’m going to cover the most important thing about this setting the perspective eyes we will also look at what are the things Django admin actually gets right so the things that it lacks doesn’t really matter once you start doing customizations you will know to make a clever if you look at it rightly and you will also find a lot of external application that aid you and doing it in and then we will look at some of the applications that try to enhance that man in the true spirit that man was meant to be if the time is remaining we will go through what are the things that Django admin can use for some in the next versions I know I don’t have to explain it to this audience but we all know that software Spanish spectrum on the one hand you have the GCC and assemblies that change the bits on the hardware that pythons that do compilation you unroll the loop speed at runtime but some of the smartest people work on this therefore we don’t have to work on this infrastructure at the rest of the people work on the business application part of it so I can solve the real world problems thanks to very neat sort of sections of build we can stand on the shoulder of the gains we can join Django admin using pi PI so the job of the frameworks is obviously to understand the common theme that exists and to create transparent abstractions Django is a framework Django admin is also a framework I don’t mean maybe an application it’s a part of the Django framework yes it’s a part of the Django contrabass it has all the aforesaid mentioned flaws yes but most importantly admin is an idea it’s a wonderful idea it was the first of its kind we will get into the technical aspects of that meant but please bear with me when I try to make this point from an end-user point of view why that admin is really a wonderful technology because it is important the technology of solutions we try to build and primarily Modi to motivate her to solve a real-world problem I don’t hear maybe to solve some first of all problems but mostly DLL problems so all of the programs use abstraction inheritance admin does it at a very high level if not is most applications themselves are similar that enabling somebody with our environment easy to tell up in order to do a basic tasks themselves can mean really a lot you know reducing the cost of maintenance and development today’s no time-to-market is everything we also know the mythical man-month you cannot just go and adding the heir-apparent a project not to make it really fast in several cases having more developers in the project could actually make it slow so a software that offers abstraction at the level of Django admin which is at the level of application itself was surely hidden making it faster and costing it better I’m not sure if before Zion government anything quite like this existed yes there were applications their reusable absolute appliable ones but the ability to bootstrap a working application this is the first surely a lot for the frameworks have this today but credit where it is due Django admin was indeed the first which I believe is the reason when people are here today because they bought into that idea you know the release of the Django itself was influenced by rails right radius did that by credit by scaffolding today even the most popular application admin application so all the frameworks have something like Sangha which is a clear endorsement of the direction it was headed yes there are a few exceptions notable people like you Don tend to do both development as well as design but mostly the developer thing to do a UI

even those people who ever do it who do both of them try to do it at alternative times of course you have booster from the like today to address similar problems where a developer didn’t know much of the UI you can eliminate padding and all of those CSS stuff by trying to include the class name span for span C’s but he still cannot do away with actually writing the HTML the tables etcetera look at the Django specifically Django and is that core if it is core it’s only specification its specification if it is core it’s only incidental you write what are the fields heads what are the fields what should be by this one you will get an entire application with the validation so this qualifies is an application for any definition of application I know a lot of database viewers exist but they do not allow you to implement the business logic and you cannot add validation so for any definition of any standard definition of application Django admin qualifies as the application this is the simplest application you can write so Django already clearly recognizes that the design is different from development by creating a templating language that is so independent of Python you don’t have to write code in fact you should not you’re not allowed to write any Python code inside this is an apartment the same idea further so that was rather longer than I thought it should be but I hope it lies the point is trying to make that mean despite it is shortcoming whatever it is even though it is quite old already and so many frameworks copy it it’s in its idea it’s really wonderful technology because conceptually and philosophically the problem it tries to solve are the problems that exist a really profound problems if you’re going to take one thing from the start with that with this well admin has its shortcomings and there are ways to overcome those but when the idea has been really powerful and self Django and its uses really well you know a lot of people here don’t really think of it as such a big deal that’s why if you look and look in the Google Analytics a lot of the popularity of admin has been so much more outside of the United States than it is here since a lot of new people may not even have used that much so far because of all the wrong long conceptions about it I want to briefly go over explain what what that means to those that don’t know about it here is one of the simpler admin files you extend the model admin that is built in the Django admin that extends the Django admin module you then set one of its many class parameters into the values you what there are many of them the commonly used ones are less display this displays the corresponding model attribute in list interface that could be a database column or a method similarly you have read only the fields in the form I know the Dani session is not there but if you attended a session or any other similar what you can do with the forms kung-fu you know you can do literally anything therefore since you can provide your own forms Django admin just through the forms come for itself we can get a long way using the Django admin because just an interface to that and before even you get to enable all of this stuff you enable that mean itself in the settings or pie in the install applications and then you uncomment a few lines in the by that one of them is appointing it pointing to the particular admin URL the other one is you call the admin rotor to discover what does the autodiscover do it goes through all of the installed application so on year enrollment and it gets that which is specified in the settings our PI and it gets all that member PI files of each of those so after the other things that admin has a list filter which you can see there I’m sure you already know about it we really don’t want to get into much of this one so and then then there is that min actions which is a which is basically you select the different objects and select an action you want to apply to all of those models you can also include the custom JavaScript for specific model template you’re dealing with mezzanine makes a very good use of the custom JavaScript inclusion don’t even the dragon trough and manage the hierarchy so that was a quick overview of what that meant it does knowing what we know so far how do we customize the Django admin what all does exist what all does exist we know that there is templates and then there is the model admin class something there is a custom media we can customize each and every one of these some of the advanced changes require you to customize all of this but when knowing certain things about how that main works you can go a long way by just using some of the techniques rather than trying to do everything for each and every requirement so how do you do a little more customization what we extend is a model admin which is extended in turn by which which extends the base model at men the base model at men specifies a lot of fields the raw ID fields the filter vertical horizontal etcetera so you can specify values to each of these as you know if you specify the fields and exclude correspondingly they will be displayed in the object page as it

stands practice like Daniel Green for inventions in inspector which is the best practice of a tango admin you know it’s generally a good avoid using exclude returned to you and use fields instead because when you add more fields the model is one come back to bite you unless if you are using an abstract more learning-based cassity’s so the very small admin most notably also has some of the permission related of methods that you override in the long run it is better to understand and customize the permissions itself than to try and override the base you know the permission methods for individual model I’d mean that you are creating because but if you have any specific requirement you can always go out and override those permission methods on each of the model element that you create so similarly the model admin extends the base model admin as we discussed it that’s a lot more fields which also you can change there are search fields date hierarchy attribute save as in lines progenitor and all of this stuff I’m sure we can look up what each of those do so list filter a list filter as we saw earlier enables you to filter on a specific database model so all of this is really well-written source so I really encourage you to look into the source don’t be afraid this is all very good code you will be able to look at look up whatever you wanted to in the source of the Jango and then admin site is the thing which creates the site itself so this is the pretty good thing is all of these are pretty well documented admin site has a lot of templates that you can override so the so basically there is base model admin model admin admin admin site which define a lot of attributes which you can override in order to provide the values that you want so you can always provide the specification thing this is the field that I want hell is display on and all of that stuff but that really doesn’t go a long way because many times you don’t know at one time whether you want to what template you have you you have to provide whether the user has the permission to something or whether you have to send an email to someone so the very good thing is that all of these have a corresponding dynamic method counterpart so the Django admin calls during each request at the runt all of these methods in order to find out the values of those statically set attributes so what does this mean to you you can override the model admin and then override these specific methods to get the data to get the values for those static parameters that you have already defined if these are these define these take precedence over the static parameter that you’ve set and they are ignored so how do you create class filters as of Django 1.4 in order to create a filter you just extend a simple list filter and then you define two attributes to it sorry two methods on it one of them is I look up one of them is the query set they look up ukq what you want to display in the filter side of it and then in the query set you find you actually return the data that you want to display by filtering and excluding whatever you want oh this is as simple as it gets in order to create a list filter similarly adding custom actions is also pretty simple you overwrite you add a new method to the model admin and then set a short description on it so that you see that in a drop-down name and then you add it as an action attribute also so now what if you want to change the template customize the template if you want to add a lot of variables into the temp camp template you see that there is a each and all that in all of the dynamic methods we saw some of them will add view change view etc each of these have extra context of extra context parameter so you override the add you do i and was issues for example antenna

be something that everybody a together

so some of these industry used present military problems actually started I mean I I think conceived today in Detroit a million profit if you know we might expected it but I used to blog about for questions we can take that when I let him answer that if there’s any questions am i here is necessary hi I just have a quick question you mentioned the bootstrap scanning for the admin and that there’s so many packages right now do you have a favorite or are they all just too new to really like only I have more than one favorite like I don’t want to pick a name because it might be better if you don’t know what is going to happen tomorrow I try to I tend to use several of them at different projects I like different things about different ones I don’t want to pick up favorite right now can you can you tell me secretly your smallest no good take them out all of them and then I indicate this already how many folks are there how many people are using it if you know you’re building this out do you have recommendations that you touch briefly on permissions and that you could override the permit existing permissions but you could start building your own so do you have any more specific recommendations on if you want to build something up that you’re it sure is secure is sure you’re not going to be like oh admins doing stuff that I didn’t realize like you know how would you handle that that side of things okay so is the question when would you use that man versus when would you build your own yes specifically began like locking locking stuff down making sure the wrong people don’t do the wrong things so my answer to that would be not every application in the world is a social networking application

where you have a lot of people coming or into the front end I’m doing a lot of tasks and very hard to configure a few things in the backend for those kinds of things admittedly to be used only for internal users but there are a lot of real-world applications where a lot of people want to log into your system mint or some data etc if that is all it takes there’s no reason you should not be using Atman there are very good ways in order to configure the permissions and then change the templates and that any hooks and I believe and I recommend that we should need more such standardized views in the future but for whatever exists if your work gets done using those things I don’t think figuring out how to do the permissions etc is going to be a big task so you would use the standard this thing in tools not okay that’s right thank you everyone