CodexBloom - Programming Q&A Platform

Django Admin Custom Action Not Triggering When Using Async Views

👀 Views: 11 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-14
django asyncio admin Python

I'm currently integrating async views in my Django project (using Django 3.2) and I've encountered an scenario where the custom admin actions are not executing as expected. I have a model `MyModel`, and I created a custom admin action to mark selected items as archived. The action works perfectly when the views are synchronous, but when I switch to an async view, it seems to skip the action altogether without any errors. Here's the code for the custom admin action: ```python from django.contrib import admin from django.utils.translation import ngettext class MyModelAdmin(admin.ModelAdmin): actions = ['archive_selected'] async def archive_selected(self, request, queryset): if queryset.count() == 0: self.message_user(request, 'No items selected.') return await queryset.update(status='archived') self.message_user(request, ngettext( '1 item was successfully archived.', '%(count)d items were successfully archived.', queryset.count() ) % {'count': queryset.count()}) admin.site.register(MyModel, MyModelAdmin) ``` I've also looked into using Django's built-in decorators to facilitate async views, but they don't seem to apply to admin actions. I see no errors in the console or logs, and the action button appears, but when I select items and trigger the action, it simply refreshes the page without performing the update. I've tried checking various settings, including `ASGI` and `WSGI` configurations, but I need to seem to pinpoint the scenario. I'm using `daphne` as the ASGI server. Are async functions not compatible with Django admin actions? What can I do to make this work?