For those who say, "I've never customized the Django admin page." This is a beginner's edition.
It seems that it can be used without customization, but when people other than engineers touch it, is it a little lonely by default?
Rather than a detailed explanation, it is a style that raises good terms quickly when you google. It's hard to put it together properly. .. ..
Django documentation https://docs.djangoproject.com/en/3.0/ref/contrib/admin/
Basically, create a subclass of the ModelAdmin
class and describe the customization contents.
For example, if you want to customize a model named SampleModel, do the following:
admin.py
from django.contrib import admin
#Normally, name it "model name + Admin"
class SampleModelAdmin(admin.ModelAdmin):
'''
Describe the customization contents in this class
'''
#Apply SampleModelAdmin to SampleModel
admin.site.register(SampleModel, SampleModelAdmin)
From this point onward, write only the description in the ModelAdmin class. For example
def hoge():
return
If you write, in fact
admin.py
class SampleModelAdmin(admin.ModelAdmin):
def hoge():
return
Please read as.
The column name is displayed at the top of the record list, but let's set what column you want to display. The following is an example when the model has fields called text and updated_at.
list_display = ('__str__', 'text', 'updated_at') #Display these three columns
Next, if you want to display a value that is not in the field.
list_display = ('__str__', 'text', 'updated_at', 'get_owner_name')
def get_owner_name(self, obj):
#The second argument obj represents the record itself
return "I am owner"
get_owner_name.short_description = 'Owner name'
This will add a column called "Owner Name", which will display "I am owner" on all records.
There is a search box at the top, but you can narrow down the search target.
# SampleModel.text, SampleModel.updated_at, SampleModel.owner.Search for username
search_fields = ('text', 'updated_at', 'owner__username`)
By default, there should be an operation called "Delete selected
actions = ['my_custom_action']
def my_custom_action(self, request, queryset):
#Something processing
return None
my_custom_action.short_description = 'Action only for me'
If you set return None
, nothing will be done when the operation is completed, but for example, you can return a template and perform page transitions.
Write this in the original Model
class instead of ModelAdmin
.
class SampleModel(models.Model):
class Meta:
verbose_name = 'Sample model'
verbose_name_plural = 'List of sample models'
list_display_links = ('text',)
We have introduced the customization of the search target, but you can also customize what kind of search is actually performed when a search word (query) is given. This will be lengthy, so I'll just give you an overview, but define the following function to override the search process.
def get_changelist(self, request, **kwargs):
If you can customize the above, wouldn't you be satisfied at first? I will write about customizing the admin page template itself and page transitions within admi in another article.
Recommended Posts