How to create Django Admin actions with and without intermediate page

How to add Django Admin action that just runs a function

# your_app/admin.pyfrom django.contrib import admin
from .models import Playlist
from .tasks import parse_playlist
class PlaylistAdmin(admin.ModelAdmin):
actions = ['parse']
def parse(self, request, queryset):
for playlist in queryset.all():

self.message_user(request, "Playlists will be reparsed soon")
  1. @admin.register — this is a decorator that prevents you to write, PlylistAdmin). For me, it is cooler way, but not everyone agrees with me:
  2. actions = ['parse'] — special ModelAdmin property where you should put the names of methods that you want to be included in Django Admin actions.
  3. def parse(self, request, queryset) — There are a lot of tutorials on the Internet where these methods for Django Admin were written outside of the ModelAdmin class. I don’t think that this is a cool way — you can “attach” these methods to the class.
  4. queryset.update(is_parsed=False) — I just change the Playlist.is_parsed field to False because my playlist will be parsed again really soon.
  5. parse_playlist.delay(url=playlist.url) — I have a Celery task called parse_playlist and in this line, I detach this task with .delay() method. Nothing special.
  6. self.message_user(…) — This is the message that will appear to Django Admin users when the parse method ends. It is cool to add there some numbers and stats on just run tasks.

How to create a Django Admin action with intermediate page to provide action params

  1. Select users from admin panel
  2. Click on my broadcast action and provide with broadcast message text
  3. Press run and let background tasks do their job.
  1. your_app/
  2. your_app/
  3. your_app/templates/admin/broadcast_message.html

This is my file

# your_app/
from django import forms
from .models import Bot
class BroadcastForm(forms.Form):
_selected_action = forms.CharField(widget=forms.MultipleHiddenInput)
broadcast_text = forms.CharField(widget=forms.Textarea)
bot = forms.ModelChoiceField(Bot.objects)
  1. _selected_action — This will be used to transfer selected items data from a Django Admin Form to the code.
  2. broadcast_text — The input text of my broadcast message. I will take this text and broadcast it to my Telegram bot users
  3. bot — In my backend, I have several Telegram bots. That is why I want to select the Bot instance which will broadcast the message.

This is my file

The Intermediate page




Daniil Okhlopkov

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Note Templates

3 Best Practices When Recovering SQL Server Databases

A Brief Guide into the Java Subject from Assignment Help Providers’ Perspective

java assignment help

Migrate BI Report from One Server to Another Server via OIC

READ/DOWNLOAD*% Portable Shell Programming: An Extensive Collection of Bourne Shell Examples FULL…

Create bitcoin address using java in 6 steps

Linear Regression: From math to code

How to recursively reverse a linked list

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dan Okhlopkov

Dan Okhlopkov

Daniil Okhlopkov

More from Medium

Django Rest API in 30 Minutes or so

Django website night mode: The cleanest and easiest way.

A Simple CRUD Application with Django

Django Social Authentication: Sign In With GitHub