[PYTHON] Customize the model page on Django's admin screen

Introduction

This article is the 10th day of Django Advent Calender 2019

By default, Django comes with a handy admin screen, but we'll customize it to make it even more convenient.

table of contents

--Display column --Link display column --Custom column --Related key column --Sort --Simple editing --Search field --Filter Customize the model page on Django's admin screen --Date navigation --Add custom actions --Delete existing action --Link column of related model

Customize list (change_list)

** Display column **

@admin.register(Book)
class BookModelAdmin(adminModelAdmin):
    List_display = ("title", "publisher", "price")

** Link display column **

@admin.register(Book)
class BookModelAdmin(adminModelAdmin):
    List_display = ("title", "publisher", "price")
    list_display_links = ("title", "price")

** Custom column **

@admin.register(Book)
class BookModelAdmin(adminModelAdmin):
    List_display = ("title", "publisher", "price_dollar")

    def price_dollar(self, obj):
        return obj.price * 0.0092

** Related key column **

@admin.register(Book)
class BookModelAdmin(admin.ModelAdmin):
    list_display = ("title", "publisher", "price", "get_establishment")

    def get_establishment(self, obj):
        return obj.publisher.establishment
    get_establishment.short_description = "publisher establishment"

sort

@admin.register(Book)
class BookModelAdmin(admin.ModelAdmin):
    list_display = ("title", "publisher", "price")
    ordering = ("-price",)

** Simple editing ** It will be possible to edit on the list page. Duplicates with list_display_links cannot be edited.

@admin.register(Book)
class BookModelAdmin(admin.ModelAdmin):
    list_display = ("title", "publisher", "price")
    list_display_links = ("title",)
    list_editable = ("publisher", "price")

** Search field **

@admin.register(Book)
class BookModelAdmin(admin.ModelAdmin):
    list_display = ("title", "publisher", "price")
    search_fields = ("title",)

filter

@admin.register(Book)
class BookModelAdmin(admin.ModelAdmin):
    list_display = ("title", "publisher", "price")
    list_filter = ("publisher",)

date

@admin.register(Book)
class BookModelAdmin(admin.ModelAdmin):
    list_display = ("title", "publisher", "price")
    date_hierarchy = "publish_date"

** Add custom action **

@admin.register(Book)
class BookModelAdmin(admin.ModelAdmin):
    list_display = ("title", "publisher", "price")
    actions = ["make_published"]

    def make_published(self, request, queryset):
        queryset.update(publish_date=datetime.now())

    make_published.short_description = "Mark selected book as published"

** Delete existing action **

@admin.register(Book)
class BookModelAdmin(admin.ModelAdmin):
    list_display = ("title", "publisher", "price")

    def get_actions(self, request):
        actions = super().get_actions(request)
        if 'delete_selected' in actions:
            del actions['delete_selected']
        return actions

** Place a link to jump to the list of other models ** _ I'm investigating because I think there is another good way. _ I feel like I should do something with _ModelAdmin or ChangeList. _

@admin.register(Book)
class BookModelAdmin(admin.ModelAdmin):
    list_display = ("title", "publisher", "price", "price_dollar", "get_year")
    list_filter = ("publisher",)

@admin.register(Publisher)
class PublisherModelAdmin(admin.ModelAdmin):
    list_display = ("name", "link_to_book")

    def link_to_book(self, obj):
        link = reverse("admin:shop_book_changelist")
        return format_html(
            '<a href="{}?publisher__id__exact={}">{}</a>', link, obj.pk, obj.name
        )

at the end

This time, I customized the model list display part of the management screen. The customization itself can be customized to the extent that there are no untouchable parts such as edit screens and form templates. Let's play with it.

Django: Documentation: admin site

Recommended Posts

Customize the model page on Django's admin screen
Implement the autocomplete feature on Django's admin screen
Try Ajax on the Django page
[Django] Correct the plural form of the model name on the management screen
The story of failing to update "calendar.day_abbr" on the admin screen of django
[Django] Want to customize your admin page?
How to make only one data register on the Django admin screen
Let's simulate the Izhikevich neuron model on the web!
Make any key the primary key in Django's model
How to filter foreign keys that can be selected on the Django admin screen
Make the model a string on a Django HTML template
Power on/off the USB port on the RaspberryPi 4 Model B