So I’ve been working for a couple days now on setting up a pretty basic bulk edit action for my server that consists of selecting all of the objects you want to change a designated field on, being redirected to an intermediate page, entering a new integer value for the designated field, and then clicking accept to make the changes to all of the objects. I tried to use the bulk delete that comes pre-installed with all models as a template to make my action, form, and template for the intermediate page, and I don’t get any errors, but when I submit my changes on the intermediate page, I’m redirected to the objects list page and my action is not run again.
I’m not sure how to get the redirect from the template to correctly call my action after it returns, I’ve tried a lot of things and nothing seems to work. I’ll post the code and screenshots below:
def change_min_interval(self, request, queryset):
form = None
opts = self.model._meta
app_label = opts.app_label
if not self.has_change_permission(request):
raise PermissionDenied
using = router.db_for_write(self.model)
if 'apply' in request.POST:
form = self.ChangeMinIntervalForm()
if form.is_valid():
new_interval = form.cleaned_data['new_interval']
rows_updated = queryset.update(min_interval=new_interval)
plural = ''
if rows_updated != 1:
plural = 's'
self.message_user(request, "Successfully changed min_interval to %s on %d node%s." % (new_interval, rows_updated, plural))
return None
if len(queryset) == 1:
objects_name = force_text(opts.verbose_name)
else:
objects_name = force_text(opts.verbose_name_plural)
if not form:
form = self.ChangeMinIntervalForm()
context = dict(
self.admin_site.each_context(request),
app_label=app_label,
title="Change min_interval",
objects_name=objects_name,
nodes=queryset,
opts=opts,
new_interval_form=form,
)
request.current_app = self.admin_site.name
return TemplateResponse(request, 'admin/change_min_interval.html', context)
change_min_interval.short_description = "Change Minimum Interval"
class ChangeMinIntervalForm(forms.Form):
new_interval = forms.IntegerField()
{% extends "admin/base_site.html" %}
{% load i18n admin_urls %}
{% block bodyclass %}{{ block.super }} app-{{ opts.app_label }} model-{{ opts.model_name }} change-min-interval-confirmation{% endblock %}
{% block breadcrumbs %}
<div class="breadcrumbs">
<a href="{% url 'admin:index' %}">{% trans 'Home' %}</a>
› <a href="{% url 'admin:app_list' app_label=opts.app_label %}">{{ opts.app_config.verbose_name }}</a>
› <a href="{% url opts|admin_urlname:'changelist' %}">{{ opts.verbose_name_plural|capfirst|escape }}</a>
› {% trans 'Change min_interval' %}
</div>
{% endblock %}
{% block content %}
<p>Enter new minimum interval to apply:</p>
<form action="" method="post"> {% csrf_token %}
<div>
{{ new_interval_form }}
<p>The new minimum interval will be applied to:</p>
<ul>{{ nodes|unordered_list }}</ul>
<input type="hidden" name="action" value="change_min_interval" />
<input type="submit" name="apply" value="{% trans "Apply Minimum Interval" %}" />
<a href="#" onclick="window.history.back(); return false;" class="button cancel-link">Cancel</a>
</div>
</form>
{% endblock %}
Selecting node to run the action on.
Selecting new min_interval for the node
Lack of results after submitting the form.
Shell outputs throughout this proccess.