2

Problem

I have a question on how to save in editable listviews in the django-admin. I use django 1.11 with python 3.6.

Suppose I have a list of 100-200 Events in a model. In order to bulk edit a boolean field for all entries, I register the model in admin-view as editable list (see code below).

For convenience, I slice the task and display 30 entries per page by using the pagination. If I switch to the next page without pressing the save button, the changes will be lost.

Now: how can I prevent loss of my input? Is there a django method to save automatically? I can imagine three ways, but I don't know how to implement it in django.

  1. Instant save for the respective entry, when its checkbox is checked.
  2. Hook on the pagination buttons: When I press any page number, the changes are saved.
  3. A Popup with warning or save button, when I leave the page without having saved.

Has anybody a suggestion or code snippets for any of the three ways? Or a suggestion for different approach?

Thank you!

Minimal Example:

from django.db import models
from django.contrib import admin

class Event(models.Model):
    name = models.CharField(max_length=30)
    description = models.CharField(max_length=30)
    relevant = models.BooleanField(default=False)
    ...

    def __str__(self):
        return '%s %s'  % (self.name, description)


class Event_Admin(admin.ModelAdmin):
    list_display = ('name', 'description', 'relevant')
    list_editable = ('relevant')

    list_per_page = 30

admin.site.register(Event, Event_Admin)
uweremer
  • 21
  • 2
  • You need some javascript to pick it up and save to the db. I'd go with option 1, instant save to db. You could write your own script and send an AJAX request. Or maybe look at grapelli or django-admin-tools, not sure if they do what you want. – chasmani Oct 20 '17 at 19:02
  • Did you ever figure this out? – James Parker Apr 29 '22 at 16:31

0 Answers0