How do I trigger a Celery task from Django admin?

北城以北 提交于 2021-02-11 07:56:11

问题


I have a model called Publication and I'd like to add a button to the list view in Django Admin which would allow triggering a Celery task.

admin.py:

from django.contrib import admin
from .models import Publication


class PublicationAdmin(admin.ModelAdmin):
    change_list_template = "variants/admin_publication_list.html"

    def update(self, request):
        # trigger task
        # redirect to admin list

admin.site.register(Publication, PublicationAdmin)

variants/admin_publication_list.html:

{% extends 'admin/change_list.html' %}

{% block object-tools %}
  <li>
    <a href="/admin/variants/publication/update/">
      Update
    </a>
  </li>
  {{ block.super }}
{% endblock %}

However when I press a button I only get a notice:

Publication with ID “update” doesn’t exist. Perhaps it was deleted?

来源:https://stackoverflow.com/questions/61519440/how-do-i-trigger-a-celery-task-from-django-admin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!