Is it possible to create a custom admin action for the django admin that doesn't require selecting some objects to run it on?

django-admin(4.1)中自定义了一个action,
(1)问题一,怎么实现定时触发此action;(五分钟执行一次)

(2)问题二:在触发action时,怎么在自定义action中设置默认为全选,解除action限制默认的(请选中至少一个选项!)

For #1, you don’t do this sort of thing in the admin. If you want a process to run on a scheduled basis, the easiest way is to create a custom management command and schedule it either through Celery Beat or cron.

For #2, again, this isn’t something to do in the admin. From Admin actions | Django documentation | Django

The basic workflow of Django’s admin is, in a nutshell, “select an object, then change it.”

and

In these cases, Django’s admin lets you write and register “actions” – functions that get called with a list of objects selected on the change list page.

I’d create a separate view for this, or possibly a custom command depending upon whether this is related to your first question.

— Using Google translate:

对于 #1,您不会在管理员中执行此类操作。 如果您希望进程按计划运行,最简单的方法是创建自定义管理命令并通过 Celery Beat 或 cron 计划它。

同样,对于 #2,这不是在管理员中要做的事情。 来自 Admin actions | Django documentation | Django

Django 管理的基本工作流程简而言之就是“选择一个对象,然后更改它”。

在这些情况下,Django 的管理员允许您编写和注册“操作”——使用在更改列表页面上选择的对象列表调用的函数。

我会为此创建一个单独的视图,或者可能是一个自定义命令,具体取决于这是否与您的第一个问题相关。

1 Like