Dieser Artikel ist der Artikel zum 10. Tag des Django Adventskalenders 2019.
Standardmäßig verfügt Django über einen praktischen Administrationsbildschirm, den wir jedoch anpassen, um ihn noch komfortabler zu gestalten.
** Spalte anzeigen **
@admin.register(Book)
class BookModelAdmin(adminModelAdmin):
List_display = ("title", "publisher", "price")
** Linkanzeigespalte **
@admin.register(Book)
class BookModelAdmin(adminModelAdmin):
List_display = ("title", "publisher", "price")
list_display_links = ("title", "price")
** Benutzerdefinierte Spalte **
@admin.register(Book)
class BookModelAdmin(adminModelAdmin):
List_display = ("title", "publisher", "price_dollar")
def price_dollar(self, obj):
return obj.price * 0.0092
** Verwandte Schlüsselspalte **
@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"
Sortieren
@admin.register(Book)
class BookModelAdmin(admin.ModelAdmin):
list_display = ("title", "publisher", "price")
ordering = ("-price",)
** Einfache Bearbeitung ** Es ist möglich, auf der Listenseite zu bearbeiten. Duplikate mit list_display_links können nicht bearbeitet werden.
@admin.register(Book)
class BookModelAdmin(admin.ModelAdmin):
list_display = ("title", "publisher", "price")
list_display_links = ("title",)
list_editable = ("publisher", "price")
** Suchfeld **
@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",)
Datum
@admin.register(Book)
class BookModelAdmin(admin.ModelAdmin):
list_display = ("title", "publisher", "price")
date_hierarchy = "publish_date"
** Benutzerdefinierte Aktion hinzufügen **
@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"
** Vorhandene Aktion löschen **
@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
** Platziere einen Link, um zur Liste der anderen Modelle zu springen ** _ Ich untersuche, weil ich denke, dass es einen anderen guten Weg gibt. _ Ich habe das Gefühl, ich sollte etwas mit _ModelAdmin oder ChangeList machen. _
@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
)
Django: Documentation: admin site
Recommended Posts