Welcome to SQLite DB Q&A Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to ask a question about the list_editable column in django-admin. If I have something like this:

class fooAdmin(admin.ModelAdmin):
    list_display = ('name', 'age', 'boolean_field')
    list_editable = ('boolean_field')

is it possible that when i enable (or disable) this list_editable field i trigger something that modify other table in database? e.g. if boolean_field became checked and I save it, automatically add or delete a row in another table? Thanks in advance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.4k views
Welcome To Ask or Share your Answers For Others

1 Answer

For your use-case, an admin action may be more appropriate.

The user would select the rows that they would like to modify on the change list page and then choose the action to perform on those rows. For illustration:

change list action example

Let's say you want to be able to select employees in the django admin and mark them as "On Vacation" or "Off Vacation". Here's what the vacation model may look like:

# models.py
class Employee(models.Model):
    name = models.CharField(max_length=255)
    age = models.IntegerField()

    def is_on_vacation(self):
        """ Returns true if the employee is on vacation, otherwise false. """
        return self.vacation_set.filter(end_date__isnull=True).exists()

class Vacation(models.Model):
    employee = models.ForeignKey("Employee")
    start_date = models.DateTimeField(auto_now_add=True)
    end_date = models.DateTimeField(blank=True, null=True)

In the admin.py we'll setup an action to start and end vacations for employees:

# admin.py
from django.utils import timezone

class EmployeeAdmin(admin.ModelAdmin):
    # Adding is_on_vacation here will show a true/false column in the change list
    # that displays the output of the Employee.is_on_vacation method.
    list_display = ("name", "age", "is_on_vacation")

    def start_vacation(self, request, queryset):
        for employee in queryset:
            Vacation.objects.create(employee=employee)

        self.message_user(
            request,
            f"Successfully started vacations for {len(queryset)} employees",
        )

    start_vacation.short_description = "Start vacations for selected employees"

    def end_vacation(self, request, queryset):
        for employee in queryset:
            vacation = employee.vacation_set.filter(end_date__isnull=True).first()
            vacation.end_date = timezone.now()
            vacation.save()

        self.message_user(
            request,
            f"Successfully ended vacations for {len(queryset)} employees",
        )

    end_vacation.short_description = "End vacations for selected employees"

Now in the django admin, when you select employees and select the start/end vacation actions, the methods above will update objects in the database and the change-list view will show updated values for the is_on_vacation column.

Note: The example code above does not perform any error checking, and should be optimized to make bulk queries.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to SQLite DB Q&A Community for programmer and developer-Open, Learning and Share
...