Django jet admin-为每行添加按钮

Django jet admin-为每行添加按钮,django,django-admin,django-jet,Django,Django Admin,Django Jet,我想创建一个删除表中选定行的按钮(每行1个按钮) 管理员 from django.contrib import admin from import_export.admin import ImportExportModelAdmin from import_export.admin import ImportExportMixin from .models import Applicant class ApplicantAdmin(ImportExportModelAdmin, admin.

我想创建一个删除表中选定行的按钮(每行1个按钮)

管理员

from django.contrib import admin
from import_export.admin import ImportExportModelAdmin
from import_export.admin import ImportExportMixin
from .models import Applicant


class ApplicantAdmin(ImportExportModelAdmin, admin.ModelAdmin):

    list_display = ('Name', 'DOB', 'PhoneNumber', 'Address', 'Batch',
                   'created_at', 'updated_at',)
    list_filter = ('Name', 'Address', 'Batch', 'created_at', 'updated_at',)
    list_per_page = 10
    # actions = [transferdata, ]


# Register the admin class with the associated model
admin.site.register(Applicant, ApplicantAdmin)
models.py

from django.db import models
from django.utils import timezone

class Applicant(models.Model):
    id = models.CharField(max_length=10).primary_key
    Name = models.CharField(max_length=50)
    DOB = models.CharField(max_length=10)
    PhoneNumber = models.CharField(max_length=20)
    Address = models.CharField(max_length=200)
    Batch = models.CharField(max_length=200)
    created_at = models.DateTimeField(default=timezone.now)
    updated_at = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return self.Name

我已经知道django jet为这个工具提供了一个下拉菜单,但是对于整个表(即不是每行)

这个问题是通过在所需的admin类中创建一个函数来解决的

admin.py

import django.contrib import admin
import import_export.admin import ImportExportModelAdmin
import import_export.admin import ImportExportMixin
import .models import Applicant

class ApplicantAdmin(ImportExportModelAdmin, admin.ModelAdmin):
    list_display = ('Name', 'DOB', 'PhoneNumber', 'Address', 'Batch',
               'created_at', 'updated_at',)
    list_filter = ('Name', 'Address', 'Batch', 'created_at', 'updated_at',)
    list_per_page = 10
    # actions = [transferdata, ]

    @staticmethod
    def action_button(self):
        # assuming the url is saved as 'button_url'
        # enter the url to be parsed when the button will be clicked and name the button
        return format_html('<a class="button" href="%s">(name of the button)</a>' % button_url)

# Register the admin class with the associated model
admin.site.register(Applicant, AdminApplicant)
import django.contrib导入管理
导入导入\ u export.admin导入导入导入端口模型管理员
import\u export.admin import ImportExportMixin
进口。模型进口申请人
类应用程序管理员(ImportExportModelAdmin,admin.ModelAdmin):
列表显示=(‘名称’、‘DOB’、‘电话号码’、‘地址’、‘批次’,
'已创建','已更新',)
列表过滤器=('Name','Address','Batch','created_at','updated_at',)
每页列表=10
#操作=[transferdata,]
@静力学方法
def操作按钮(自身):
#假设url保存为“按钮\ url”
#输入单击按钮时要分析的url并命名按钮
返回格式\u html(“”%button\u url)
#向关联模型注册admin类
管理员站点注册(申请人,管理员申请人)
视图.py中创建按钮的功能

import django.contrib import admin
import import_export.admin import ImportExportModelAdmin
import import_export.admin import ImportExportMixin
import .models import Applicant

class ApplicantAdmin(ImportExportModelAdmin, admin.ModelAdmin):
    list_display = ('Name', 'DOB', 'PhoneNumber', 'Address', 'Batch',
               'created_at', 'updated_at',)
    list_filter = ('Name', 'Address', 'Batch', 'created_at', 'updated_at',)
    list_per_page = 10
    # actions = [transferdata, ]

    @staticmethod
    def action_button(self):
        # assuming the url is saved as 'button_url'
        # enter the url to be parsed when the button will be clicked and name the button
        return format_html('<a class="button" href="%s">(name of the button)</a>' % button_url)

# Register the admin class with the associated model
admin.site.register(Applicant, AdminApplicant)
url.py中,在应用程序的url模式中输入url(几乎与管理类中的url相同),并调用views.py中的函数

import django.contrib import admin
import import_export.admin import ImportExportModelAdmin
import import_export.admin import ImportExportMixin
import .models import Applicant

class ApplicantAdmin(ImportExportModelAdmin, admin.ModelAdmin):
    list_display = ('Name', 'DOB', 'PhoneNumber', 'Address', 'Batch',
               'created_at', 'updated_at',)
    list_filter = ('Name', 'Address', 'Batch', 'created_at', 'updated_at',)
    list_per_page = 10
    # actions = [transferdata, ]

    @staticmethod
    def action_button(self):
        # assuming the url is saved as 'button_url'
        # enter the url to be parsed when the button will be clicked and name the button
        return format_html('<a class="button" href="%s">(name of the button)</a>' % button_url)

# Register the admin class with the associated model
admin.site.register(Applicant, AdminApplicant)