Admin

Configuración del admin

Registrar modelos

archivo admin.py

from .models import Author, Genre, Book, BookInstance

admin.site.register(Book)
admin.site.register(Author)
admin.site.register(Genre)
admin.site.register(BookInstance)

Crear Administrador

Configurar

Clases ModelAdmin

# Define the admin class en admin.py
class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'author', 'display_genre')

# Register the admin class with the associated model
admin.site.register(Author, AuthorAdmin)
# añadir al modelo Book en models.py
    def display_genre(self):
        """Create a string for the Genre. This is required to display genre in Admin."""
        return ', '.join(genre.name for genre in self.genre.all()[:3])

    display_genre.short_description = 'Genre'

list filters

class BookInstanceAdmin(admin.ModelAdmin):
    list_filter = ('status', 'due_back')

    fieldsets = (
        (None, {
            'fields': ('book', 'imprint', 'id')
        }),
        ('Availability', {
            'fields': ('status', 'due_back')
        }),
    )

Inlines

class BooksInstanceInline(admin.TabularInline):
    model = BookInstance

@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'author', 'display_genre')

    inlines = [BooksInstanceInline]

funciones especiales para el admin

from django.contrib import admin
from django.db import models
from django.utils.html import format_html

class Bookinstance(models.Model):
    ...

    @admin.display
    def estado(self):
        return format_html(
            '<i class="bi bi-book-fill {}"></i>', 
            'text-success' if self.status == 'a' else 'text-danger'
        )
Última modificación November 21, 2022: update (9d61e90)