Django-jet admin- add button for each row

830 Views Asked by At

I want to create a button that deletes the selected row in the table (1 button per row)

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

I already know django-jet that provides this facility a drop-down menu, but for the whole table (i.e. not for the each row)

1

There are 1 best solutions below

0
On BEST ANSWER

This problem by creating a function inside the required admin class.

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)

Create a function of the button in the views.py

In the urls.py enter the url (almost the same as in admin class) in urlpatterns of the app and call the function present in views.py