2022-12-28

Author: Meet

Worked on django admin section for fixing and optimizating of the deisgn application. Created a custom field lists and optimized the query results with raw_id_fields attribute which takes in the foreign key model and just fetches the id instead of all the related instance objects(drop down select option).

TIL

Created a field list page with customizable fields of the model.

from django.contrib import admin
from .models import Blog


class BlogAdmin(admin.ModelAdmin):
    search_fields = ('id', 'title',)
    list_display = ('title', 'tags',)

admin.site.register(Blog, BlogAdmin)

We can speicify the list_display tuple. Which should be a list of the model fields to be displayed in the admin section(list page). Also for foreign/onetomany relationship fields, we can speicify the table__attribute fields. The search_fields will be used for having a search bar and will look for matching search results in the provided fields. Since, this is a custom class inherited from ModelAdmin we have to speicify the pair with the actual model and the custom admin class.