Django-jet admin- 添加每行的按钮



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

admin.py

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)

views.py中创建按钮的功能

urls.py中输入应用程序的 urlpatterns 中的 url(几乎与 admin 类中的相同(,并调用views.py中存在的函数

最新更新