Python Django Tutorial: Advanced Admin Interface - Part Six

Introduction

 
Today, I will share one new part of Python Django from my tutorial series. In this part, you will learn the Advanced admin interface in Django.
I already told you the following:
Django provides many features to use in the admin panel of Django. I have already told you some basics of Django admin in my previous article. So you can use the previous article model's structure and can also create new models fo it in this advanced admin interface.
  1. According to the previous article fill up the information on the publisher model's data and save it.
     
     
  2. After saving you will interface of the publisher as below image.
     
     
  3. In the above image, you will see the publisher object. Not user information here. When you click this object you will get automatic fill up a form with information. But my main concern is fetching object name rather than publisher object.
     
  4. So now we go to models.py file and write a predefined method in models call of the publisher.
     
     
  5. Here a method def __str__(self) is used for return data when we want to access this model. When we see this model in the admin panel, it will always show the name of the publisher rather than the publisher object. Now go to the Admin panel and refresh the page of the publisher.
     
     
  6. So this depends on the return of __str__ method. We can also return anything that will always show here above image. See another demo.
     
     
  7. Here str method returns all table attribute values. So now check-in admin interface.
     
     
  8. It works. Here you see the title of the publisher is single which holds all values. Now another feature is Django shows all values with different-different columns. Now change the models.py str method that will return only name of Publisher.
     
  9. Now we go to admin.py file and write some as below image.
     
     
  10. The above code represents the class which has a list_display variable this is predefined in Django and adds attributes of Django models in it according to your choices. Now we check the admin panel.
     
     
  11. You can see all the data in different-2 columns.
     
  12. We can add searching in the admin model for search data when you have any data in that model.
     
  13. Now we go to admin.py file and write code as below.
     
     
  14. Here name, city, and country is that field that can help for search data into the admin interface.
     
     
  15. This is the search in the Django admin interface. Now we move another feature of Django that is the filter of data.
     
  16. Add code in admin.py
     
     
  17. Check-in the admin Interface.
     
     
  18. It works. Django admin also has some other features:
     
    • date_hierarchy
     
    • ordering
     
    • filter_horizontal
     
    • raw_id_fields
     
  19. Now we simply see above all feature demos in a single example.
     
     
So this is the Advanced use of the admin interface. Django Provides the best admin framework feature and also we can customize admin using user-defined methods.


Recommended Free Ebook
Similar Articles