我想创建一个按钮来删除表中的选定行(每行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 ,它为该功能提供了一个下拉菜单,但适用于整个表格(即不是针对每一行)
答案 0 :(得分:0)
通过在所需的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 中存在的函数。 >