来自管理日志中的提交按钮的POST请求

时间:2016-02-04 07:13:25

标签: django python-2.7 django-admin

class CustomAdmin(admin.ModelAdmin):
    exclude = ['is_external']

@admin.register(Submission)
class SubmissionAdmin(CustomAdmin):
    list_display = ('is_active', 'activate')

    def activate(self, obj):
        from django.utils.safestring import mark_safe
        if not obj.is_active:
            return mark_safe('<button name="Activate" button type="submit" class="btn" '
                             'value="%s">Activate</button>' % escape(obj.id))
        else:
            return mark_safe('<button name="Activate" button type="submit" '
                             'value="%s" disabled>Activate</button>' % escape(obj.id))

我在某个管理员日志列表中激活了一个按钮。单击此按钮后,它会提交整个列表并将POST请求发送到同一页面,其中activate属性为选择行的ID,我想处理该POST。我可以在这个SubmissionAdmin课程中这样做吗?

1 个答案:

答案 0 :(得分:0)

我认为您可以通过创建自定义操作

来实现这一目标
class CustomAdmin(admin.ModelAdmin):
    exclude = ['is_external']

@admin.register(Submission)
class SubmissionAdmin(CustomAdmin):
    list_display = ('is_active')

    def activate(self,request,queryset):
          for obj in queryset:
              #you can add the activate logic here

    def deactivate(self,request,queryset):
          for obj in queryset:
              #you can add the de-activate logic here

    activate.short_description = "Activate"
    activate.short_description = "De-Activate"
    actions = [activate,deactivate]

https://docs.djangoproject.com/en/1.9/ref/contrib/admin/actions/