In this chapter, you will learn about some of the features of the admin interface.
Admin Interface
Django provides many features to use in the admin panel of Django. We have already learned some basics of Django admin in the previous chapter. So you can use the previous chapter model's structure and can also create new models fo it in this advanced admin interface.
- According to the previous chapter, fill up the information on the publisher model's data and save it.
- After saving you will interface of the publisher as below image.
- 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.
- So now we go to models.py file and write a predefined method in models call of the publisher.
- Here a method def __str__(self) is used to 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.
- So this depends on the return of __str__ method. We can also return anything that will always show here above image. See another demo.
- Here str method returns all table attribute values. So now check-in admin interface.
- 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 the name of Publisher.
- Now we go to admin.py file and write some as below image.
- 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.
- You can see all the data in different-2 columns.
- We can add searching in the admin model for search data when you have any data in that model.
- Now we go to admin.py file and write code as below.
- Here name, city, and country is that field that can help for search data into the admin interface.
- This is the search in the Django admin interface. Now we move another feature of Django that is the filter of data.
- Add code in admin.py
- Check-in the admin Interface.
- It works. Django admin also has some other features:
• date_hierarchy
• ordering
• filter_horizontal
• raw_id_fields
- 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.
Summary
In the next chapter, we will learn how to use Django templates.