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.
--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
** 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
)
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