Add a Custom Action to Django Admin and Remove the Restriction of Having to Select Objects
Introduction I have to say, Django's Admin really brings a lot of convenience to development. Here I am tinkering again, this time adding a custom action You can check the official documentation for this custom Action; it's very detailed, so I won't repeat it. https://docs.djangoproject.com/en/3.0/ref/contrib/admin/actions/ UI Effect By default, after adding an action in Django, you have to select objects below before you can submit, but I don't need
目录7 个章节
Introduction
I have to say, Django's Admin really brings a lot of convenience to development. Here I am tinkering again, this time adding a custom action.
You can check the official documentation for this custom Action; it's very detailed, so I won't repeat it.
https://docs.djangoproject.com/en/3.0/ref/contrib/admin/actions/
UI Effect

By default, after adding an action in Django, you have to select objects below before you can submit. But I don't need that—I just want to simply click Start Lottery and have the prize drawn right away. Persistence pays off; I still found a solution on Stack Overflow, (SOF-oriented programming, as expected)
Next, I'll explain how to implement it~
Define the Action
Write the following code in admin.py:
@admin.register(models.PrizeLog)
class PrizeLogAdmin(admin.ModelAdmin):
actions = ['draw_prize']
def draw_prize(self, request, queryset):
pass
draw_prize.short_description = '开始抽奖'
draw_prize.type = 'warning'
draw_prize.confirm = '确认即开始抽奖并发送中奖通知~'
This will display the button, but clicking it still won't execute the code inside the draw_prize method, because Django requires at least one object to be selected before an action can run.
Next, we'll take a roundabout approach to bypass this restriction.
Remove the Restriction
Still in this Admin class, write the following code:
@admin.register(models.PrizeJoinLog)
class PrizeJoinLogAdmin(admin.ModelAdmin):
actions = ['draw_prize']
def changelist_view(self, request, extra_context=None):
try:
action = self.get_actions(request)[request.POST['action']][0]
action_acts_on_all = action.acts_on_all
except (KeyError, AttributeError):
action_acts_on_all = False
if action_acts_on_all:
post = request.POST.copy()
post.setlist(admin.helpers.ACTION_CHECKBOX_NAME,
self.model.objects.values_list('id', flat=True))
request.POST = post
return super(PrizeJoinLogAdmin, self).changelist_view(request, extra_context)
def draw_prize(self, request, queryset):
pass
draw_prize.short_description = '开始抽奖'
draw_prize.type = 'warning'
draw_prize.confirm = '确认即开始抽奖并发送中奖通知~'
draw_prize.acts_on_all = True
Note the code above: we added the changelist_view method, and in the last line we added an acts_on_all attribute to draw_prize and set it to True (that's the invincible dynamic language Python for you)
This way, when submitting, changelist_view detects that the action has an acts_on_all attribute and automatically includes all objects—equivalent to automatically checking all objects for us. Pretty neat~
Discussion Welcome
This approach doesn't feel entirely perfect though. If you experts have a better solution, feel free to leave a comment and discuss~
References
- https://stackoverflow.com/questions/4500924/django-admin-action-without-selecting-objects
- https://simpleui.88cto.com/docs/simpleui/QUICK.html#%E8%87%AA%E5%AE%9A%E4%B9%89%E6%8C%89%E9%92%AE-action
Discussion Welcome
I've compiled a series of technical articles and materials. Reply with linux, flutter, c#, netcore, android, kotlin, java, python, etc. in the backend of the WeChat official account "Programming Lab" to get related technical articles and materials. If you have any questions, you can also leave a message in the official account backend~
_EOF_
Add a Custom Action to Django Admin and Remove the Restriction of Having to Select Objects
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
轻量级消息队列Django-Q轻度体验
前言 最近做的这个项目(基于Django),需要做个功能,实现定时采集车辆定位。 这让我想起来几年前那个OneCat项目,当时我用的是Celery这个很重的组件 Celery实在是太重了,后来我做公众号采集平台的时候,又接触了Django-RQ和Django-Q这俩,前者是对RQ的封装,让RQ和Django更好的结合在一起;后者是一个全新的「多进程任务队列」组件,相比起celery很轻量,当时使用
2022-08-31 · Django · 约 1 分钟
Django的单元测试和集成测试
测试,在以前确实是经常被忽略的话题,特别是对于 Python Web 这种快速开发框架
2024-03-06 · Django · 约 1 分钟
Django统一包装接口返回数据格式
前言 最近实在太忙了,开始了一个新的项目,为了快速形成产品,我选择了Django来实现后端,然后又拿起了之前我封装了项目脚手架「DjangoStarter」。 由于前段时间我写了不少.NetCore的后端代码,对CRUD的接口开发又有了一些新的理解,所以肯定也要把DjangoStarter改造一番,改得更加顺手~ 题外话:话说我在前端终于真正用上react,不得不说 ts + react 真好用,
2022-08-26 · Django · 约 1 分钟
Django项目引入NPM和gulp管理前端资源
前言 之前写了一篇《Asp-Net-Core开发笔记:使用NPM和gulp管理前端静态文件》,现在又来用Django开发项目了,之前我搞了一个Django的快速开发脚手架「DjangoStarter」,正好给升级一下~ 关于npm和gulp我就不多重复了,之前那篇文章都写了,直接上操作 安装依赖 安装依赖的操作跟上一篇文章是一样的,不过我这里直接提供package.json文件,复制到项目根目录里
2022-04-02 · Django · 约 1 分钟
讨论
共 0 条加载评论中…