Custom Django Admin Actions

Doing bulk data actions in the admin is easy, if you know how to do it. In this video you will learn how to create your own custom admin actions so you can update a lot of data objects all at the same time.
Download HD Version next video: Namespaced Urls

resources

Admin actions

todos/admin.py

from django.contrib import admin

from .models import Task

def all_tasks(modeladmin, request, queryset):
    for qs in queryset:
        print qs.title

def complete_tasks(modeladmin, request, queryset):
    queryset.update(completed=True)
complete_tasks.short_description = 'Mark as Complete'

def incomplete_tasks(modeladmin, request, queryset):
    queryset.update(completed=False)
incomplete_tasks.short_description = 'Mark as Not Complete'

class TaskAdmin(admin.ModelAdmin):
    list_display = ['title', 'completed']
    ordering = ['created']
    actions = [complete_tasks, incomplete_tasks, all_tasks]

admin.site.register(Task, TaskAdmin)
comments powered by Disqus