Django Admin

Like a DB but with with a mouse!

ELAD SILBERRING

Master of code, God of bits

Admin What?

Let's see some screenshots

Admin Dashboard

Home of all apps and their models

(Based on permissions)

List View

All our instances in 1 page!

editable?

Item View

Lets Break PROD!

Admin Dashboard

Do you like color?

Admin initial code

# PRESENTING CODE

from django.contrib import admin
from myapp.models import Author

admin.site.register(Author)


# USING DECORATORS

from django.contrib import admin
from .models import Author


@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):
    pass

Lets customize

# PRESENTING CODE

@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):
    list_display = ["name", "birth_date"] 
    # to show all => list_display = BookAdmin._meta.get_all_field_names()
    

### what else? ###

@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):
    list_display = ["name", "birth_date", "ratings_count"]  # ratings_count is a model function
    

    @admin.display(empty_value="N/A") 
    def ratings_count(self, obj):
        return self.google_ratings + self.kindle_ratings 

But Elad, What about Item view?

# PRESENTING CODE
# MODEL

class Author(models.Model):
    name = models.CharField(max_length=100)
    title = models.CharField(max_length=3)
    birth_date = models.DateField(blank=True, null=True)
    
# ADMIN -> admin.py code


class AuthorAdmin(admin.ModelAdmin):
    fields = ["name", "title"]

## OR ##

class AuthorAdmin(admin.ModelAdmin):
    exclude = ["birth_date"]

List View

what can go wrong?

Inline Edit

@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
    list_display = ["book", "year", "rank"] 
    inlines = ["book", "year", "rank"] 

Inline Edit

@admin.register(Shelf)
class ShelfAdmin(admin.ModelAdmin):
    list_display = ["book", "year", "rank"] 
    list_editable = ["book", "year", "rank"]     
    actions = ("uppercase",) 
    
    @admin.action(description='Make selected books uppercase')
    def uppercase(modeladmin, request, queryset):
        for obj in queryset:
            obj.book = obj.name.upper()
            obj.save()
            # what's this??
            messages.success(request, "Successfully made uppercase!") 

Item View

Lets Go PM!

This can be a custom field - only dropdown

Field sets

# PRESENTING CODE
from django.contrib import admin


class FlatPageAdmin(admin.ModelAdmin):
    fieldsets = [
        (
            None, # title
            {
                "fields": ["url", "title", "content", "sites"],
            },
        ),
        (
            "Advanced options",
            {
                "classes": ["collapse"],
                "fields": ["registration_required", "template_name"],
            },
        ),
    ]

Field sets

# PRESENTING CODE

DEMO?

Not Today. But look, a squirrel!!

Multiple Admin

Sites For 1 App!

Connect To

Multiple DBs!

Run tests from UI!

DEMO?

Still no, but look at the time gotta go!!

by flow type?

Rerun task

merge data

DEMO?

YES!

QUESTIONS?

Made with Slides.com