Customize admin panel

Customize admin panel?

Customize admin panel- In order to use database  more conveniently, we need to learn how to customize admin panel. It includes adding search, filters, list-view etc. to admin panel.

Django-admin-customization
Django-admin-customization

In our previous tutorials, i have discussed a lot about creating models and admin customizing admin panel. In this tutorial i will continue the discussion about using Admin panel more conveniently.

Let’s take an example of our previous model. If you doesn’t know about models you can visit the link below, skip if you have your own model.

Django Models

What we will learn next?

  1. Insert, Delete, update model data
  2. Return model object using __str__()
  3. Arranging/ selecting fields to show
  4. List view of the fields
  5. Filter model data according to fields
  6. Adding search to admin panel
  7. Choose Editable fields

Insert model data

I will use my previous model, you can use your own model instead. After logging in to admin panel, there will be index page. You can visit the link below to learn about using admin panel:

Django admin customization

Index page includes two main parts which are:

  1. Authentication and Authorization
  2. App list

Authentication and Authorization

It contains the authenticated users and groups. You can see in the link above.

App list

There will be list of all apps in your project and their corresponding models. You further can change model data by clicking on it.

Project folder > app folder > models.py

from django.db import models

class user(models.Model):
    name = models.CharField(max_length=50 )
    Contact = models.IntegerField(unique=True)

Step 1 – After login click on model name and then add data

Home › Myapp › Users

Customize admin panel
Customize admin panel

Step 2 – Click on add button at right side

After clicking on add button , there will be new page where you can add data in fields. There will be a page like below:

Django admin customization
Django admin customization

Step 3 – Save data

Click on save button if you want to save data. Moreover if you want to add another data click on Save and add another data. After that it returns the page like below:

Django admin customization
Django admin customization

You can see in the image above, it will show every entry as an object. Instead you can use any field name to return.

Update and delete model data

You can update and delete the user object, you want. After clicking on user object, there will an window like below:

Django admin customization
Django admin customization

Return model object using __str__()

You can see in the image above, when we insert some data in models. It returns that data as a user object (1), user object (2) and so on. You can return any data instead. For example, if you want to return user names instead of this then you can. Use special function __str__ to return specific data as shown below:

Add __str__() and put column name to the return that you want to show

Project folder > app folder > models.py

from django.db import models

class user(models.Model):
    name = models.CharField(max_length=50 )
    Contact = models.IntegerField(unique=True)
    
    def __str__(self):
        return self.name

Output

Django admin customization
Django admin customization

Explanation

  1. As you can see in output above, it returns the field data whatever you select.
  2. I have two users, it shows names of both(because we have return name).
  3. Name is an string, if you want to return non-string data using __str__(). Then you have to type cast that data to string.
  4. For example to return contact number, you should do:
def __str__(self):
    return repr(self.Contact)

arranging/selecting fields to show

If we want to show only specific fields of models, or these fields should retain some other order. To do so we need to customize admin panel. We have to use admin.ModelAdmin class of Django. There will be following code in admin.py

Example

Project folder > app folder > admin.py

from django.contrib import admin
from myapp.models import user

class userAdmin(admin.ModelAdmin):
    fields =['Contact','name']

admin.site.register(user,userAdmin)

Output

Django admin customization
Django admin customization

You can see in example above, the order of the fields is now changed.

Steps

  1. Add a new class modelnameAdmin.
  2. Add variable, you want (here is fields).
  3. register the class to admin.site.register.

List view of the fields

To show the returned object as a table view (in the form of rows and columns). You just need to add new variable list_display to the userAdmin class.

Example

Add another variable list_display to the class:

 list_display = ['name','Contact']

Output

Django admin customization
Django admin customization

Filter, search,edit,model data according to fields

  1. To add filters of data in admin panel(user data), you just need to add the list_filter attribute to the class.
  2. To add search field you just need to add search_fields attribute. You can separate fields by commas if you want to search with multiple fields like username , city etc.
  3. To make field editable, you just need to add list_editable variable to admin class.
  4. Below is the final code of admin.py

Project folder > app folder > admin.py

from django.contrib import admin
from myapp.models import user

class userAdmin(admin.ModelAdmin):
    fields =['Contact','name']
    search_fields = ['name']
    list_filter = ['Contact','name']
    list_display = ['name','Contact']
    list_editable = [ 'Contact']

admin.site.register(user,userAdmin)

Output

Output: search results for ‘h’

Django admin customization
Django admin customization

Download source code

Download project on Github

So it is all about to customizing admin panel. If you have any questions regarding it then you can comment here. Thanks!!!

22 thoughts on “Customize admin panel

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. Wonderful blog! I found it while searching on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Thank you

  2. I am frequently to blogging i actually appreciate your articles. This article has truly peaks my interest. I am going to bookmark your web blog and keep checking for first time information.

  3. I just want to mention I am very new to weblog and definitely loved your blog site. Most likely I’m going to bookmark your site . You actually come with terrific article content. Thank you for sharing your webpage.

  4. Thanks for another informative website. Where else could I get that kind of information written in such a perfect way? I have a project that I’m just now working on, and I’ve been on the look out for such info.

  5. I loved as much as you’ll receive carried out right here. The sketch is attractive, your authored material stylish. nonetheless, you command get bought an edginess over that you wish be delivering the following. unwell unquestionably come further formerly again as exactly the same nearly a lot often inside case you shield this increase.

  6. Its like you learn my thoughts! You seem to understand a lot about this,
    such as you wrote the e book in it or something. I feel that you just
    could do with some p.c. to drive the message house a
    bit, but other than that, that is fantastic blog.
    A fantastic read. I will definitely be back.

  7. I liked up to you will obtain performed proper here. The caricature is tasteful, your authored subject matter stylish. nonetheless, you command get got an shakiness over that you would like be turning in the following. ill indubitably come further earlier again as precisely the similar just about very steadily inside case you shield this increase.

  8. Hey there! I just wanted to ask if you ever have any issues with hackers? My last blog (wordpress) was hacked and I ended up losing many months of hard work due to no backup. Do you have any solutions to prevent hackers?

  9. hello there and thank you for your info – I’ve certainly picked
    up anything new from right here. I did however expertise some technical issues using this website, as I
    experienced to reload the website lots of times previous to I
    could get it to load correctly. I had been wondering if your web host is OK?
    Not that I am complaining, but slow loading instances times will sometimes affect your placement in google and could damage your quality
    score if ads and marketing with Adwords. Well I’m adding this RSS to my e-mail
    and could look out for much more of your respective fascinating content.

    Ensure that you update this again very soon.

  10. Heya i’m for the primary time here. I came across this board and I
    to find It really helpful & it helped me out a lot.

    I am hoping to present one thing again and aid others like you helped me.

  11. Just want to say your article is as astounding. The
    clearness in your post is just spectacular and i could assume
    you’re an expert on this subject. Fine with your permission let me
    to grab your feed to keep updated with forthcoming post.
    Thanks a million and please carry on the enjoyable work.

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.