Django Tutorial: Admin Actions 🎯

beginner
23 min

Django Tutorial: Admin Actions 🎯

Welcome back to CodeYourCraft! Today, we're diving into Admin Actions, a powerful feature of Django that helps manage your applications efficiently.

By the end of this lesson, you'll be able to create, update, delete, and perform custom actions on your Django models. Let's get started! πŸ“

What are Admin Actions?

Admin Actions are methods defined in Django's admin.py file for specific models. They allow you to perform various tasks like creating, updating, deleting, and even custom actions on your application data through the Django admin interface.

πŸ’‘ Pro Tip: Admin Actions are a must for managing data in real-world projects, as they provide an easy and efficient way to maintain your application's database.

Creating Admin Actions: Example 1 πŸ“

Let's create an action to mark a BlogPost as featured.

  1. First, we need to import the SimpleListFilter and actions from django.contrib.admin.
python
from django.contrib import admin from django.contrib.admin.actions import Action from django.contrib.admin.filters import SimpleListFilter
  1. Next, define the action function:
python
class MakeFeatured(Action): description = "Mark selected blog posts as featured." def change_list_view(self, request, model_admin, queryset): queryset.update(is_featured=True)
  1. Then, define a custom filter for our action:
python
class FeaturedFilter(SimpleListFilter): title = "Featured" parameter_name = "is_featured" def lookups(self, request, model_admin): return ( ("Yes", "Yes"), ("No", "No"), ) def queryset(self, request, queryset): if self.value() == "Yes": return queryset.filter(is_featured=True) elif self.value() == "No": return queryset.exclude(is_featured=True) return queryset
  1. Finally, register the filter and the action in the admin.py file for the BlogPost model:
python
class BlogPostAdmin(admin.ModelAdmin): list_filter = (FeaturedFilter,) actions = [MakeFeatured]

Now, when you visit the Django admin interface for BlogPosts, you'll see the "Mark as Featured" action and the "Featured" filter.

Quick Quiz
Question 1 of 1

Which line in the code above updates the is_featured field for the selected blog posts?

Creating Admin Actions: Example 2 πŸ“

For our second example, let's create an action to delete multiple blog posts at once.

  1. Define the action function:
python
class DeleteMultiple(Action): description = "Delete multiple blog posts at once." def __call__(self, request, queryset): for obj in queryset: obj.delete() return queryset.count()
  1. Register the action in the admin.py file for the BlogPost model:
python
class BlogPostAdmin(admin.ModelAdmin): actions = [DeleteMultiple]

Now, when you visit the Django admin interface for BlogPosts, you'll see the "Delete multiple" action.

Quick Quiz
Question 1 of 1

How many blog posts are deleted by the `DeleteMultiple` action?

And there you have it! With these examples, you've learned how to create custom actions for your Django applications. Happy coding! πŸŽ‰πŸ’»

πŸ’‘ Pro Tip: Remember to customize these actions according to your application's needs for efficient data management.

Stay tuned for more Django tutorials here at CodeYourCraft! 🌟


Types for this lesson:

  • Action: A Django class representing an admin action that can be performed on a model.
  • SimpleListFilter: A Django class for creating filters for the change list view.
  • ModelAdmin: A Django class that represents an admin configuration for a model.