Skillversitas

The Django Admin

The admin is the CRUD the auto: the @admin.register the 1 line (the list, the change, the delete), the ModelAdmin the 3 option (the list_display, the list_filter, the search_fields), and the auth is the require (the login the no the 403, the permission the check). The inlines is the M2M (the 1 page the edit the related), the actions is the bulk (the 2 page the no the 1), and the fieldsets is the group (the 1 form the 3 the tab). This chapter covers the register (the 1 line), the ModelAdmin (the 6 option, the search the filter), the inlines (the M2M, the tab), the actions (the bulk), and the permission (the 4, the no the superuser).

The register and the ModelAdmin

# the REGISTER (the 1 line, the CRUD's):
from django.contrib import admin
from .models import Note

@admin.register(Note)
class NoteAdmin(admin.ModelAdmin):
    #   the list (the /admin/notes/):
    list_display = ("title", "created",
        "views")
    list_filter = ("created",)
    search_fields = ("title", "body")
    date_hierarchy = "created"
    #   the change (the /admin/notes/1/
    #     /change/):
    fieldsets = (
        (None, {"fields":
            ("title", "body")}),
        ("Meta", {"fields":
            ("created", "views"),
            "classes": ("collapse",)}),
    )
    #   the no the field (the no the
    #     the change, the read only):
    readonly_fields = ("created",)

The @admin.register(Note) is the 1 line: the /admin/notes/ the list (the title, the created), the change (the title the body the form), and the delete (the confirm the no the no) - the ModelAdmin is the 6 option: the list_display (the column, the callable the color), the list_filter (the sidebar, the date the boolean), the search_fields (the box, the icontains), the date_hierarchy (the year the month the day the link), the fieldsets (the group, the collapse the tab), and the readonly_fields (the no the change, the created) - the auth is the require: the createsuperuser the 1 user, the login the no the 403, and the is_staff is the flag (the True the access, the False the no) - the inlines is the next: the M2M the 1 page the edit.

The inlines and the actions

# the INLINE (the M2M, the 1 page):
class TagInline(admin.TabularInline):
    model = Note.tags.through
    extra = 0   #   the no the empty

#   the stack (the vertical, the the
#     the many the field):
class TagInline(admin.StackedInline):
    model = Note.tags.through
#   the the 2: the the through's
#     the no the field (the M2M's)
#   the the Tabular the wide, the
#     Stacked the tall (the the 2)

# the ACTION (the bulk, the 2 page):
@admin.register(Note)
class NoteAdmin(admin.ModelAdmin):
    actions = ["publish_all"]
    @admin.action
    def publish_all(modeladmin,
        request, queryset):
        n = queryset.update(
            published=True)
        messages.success(request,
            f"{n} published")
#   the /admin/notes/ the checkbox,
#     the dropdown, the 1 click (the
#     the no the per-1)

# the PERMISSION (the 4, the no the
#   superuser):
class NoteAdmin(admin.ModelAdmin):
    #   the no the user (the no the
    #     the view, the 403):
    def has_view_permission(self,
        request, obj=None):
        return request.user.is_staff
    #   the no the user (the no the
    #     the change, the 403):
    def has_change_permission(self,
        request, obj=None):
        return (request.user.is_staff or
            obj is None or
            obj.author ==
            request.user)

The inlines is the M2M the 1 page: the TabularInline (the wide, the 1 row the 1 tag), the StackedInline (the tall, the many the field), and the extra = 0 (the no the empty the row) - the 1 page the post plus the tags plus the author the edit (the no the 2 page). The action is the bulk: the checkbox the dropdown (the publish_all the update the 1 the SQL), the messages.success the feedback, and the no the per-1 (the 100 the 1 click the no the 100) - the permission is the 4: the has_view_permission (the list), the has_add_permission (the create), the has_change_permission (the edit, the author the self), and the has_delete_permission (the delete) - the no the superuser the require (the is_staff the flag, the permission the object) - the rule is the one: the register the 1 line, the ModelAdmin the 6 option, the inlines the M2M, the actions the bulk, and the permission the 4 the no the superuser - the next chapter's URL is the route the path the name the reverse.

The Working Rules

  • The register is the 1 line, the CRUD the auto. The @admin.register(Note) the list the change the delete, and the ModelAdmin the 6 option (the list_display, the list_filter, the search_fields, the date_hierarchy, the fieldsets, the readonly_fields) - the callable the color (the no the template), and the search the icontains (the no the exact).
  • The inlines is the M2M, the 1 page. The TabularInline the wide (the 1 row the 1 tag), the StackedInline the tall (the many the field), and the extra = 0 (the no the empty) - the through the no the field, and the 1 page the post plus the tags plus the author (the no the 2 page, the no the redirect).
  • The action is the bulk, the 1 click. The checkbox the dropdown (the publish_all the update, the 1 the SQL), and the messages.success the feedback (the no the silent) - the 100 the 1 click (the no the 100 the per-1), and the action the no the permission (the has_module_permission the check).
  • The permission is the 4, the no the superuser. The has_view_permission the list, the has_add_permission the create, the has_change_permission the edit (the author the self), and the has_delete_permission the delete - the is_staff the flag (the no the 403), and the object the no the global (the obj the arg, the check the 1).

Summary

The admin is the CRUD the auto: the @admin.register(Note) the 1 line (the list, the change, the delete, the no the view the template), the ModelAdmin the 6 option (the list_display the column, the list_filter the sidebar, the search_fields the box, the date_hierarchy the link, the fieldsets the group, the readonly_fields the no the change), the inlines the M2M (the TabularInline the wide, the StackedInline the tall, the 1 page the no the 2), the action the bulk (the checkbox the dropdown, the 1 click the no the 100, the messages the feedback), and the permission the 4 (the view, the add, the change the author the self, the delete, the no the superuser the is_staff the flag). The auth is the require (the createsuperuser, the login, the no the 403) - the next chapter's URL is the route: the path the name the reverse, the include the app the prefix, the 2 level the project the app, and the kwargs the int the pk the no the 404.