Django custom admin actions auto updating/refresh admin page
I have a an admin action that opens a pdf object from my db and updates certain fields associated with that row. How would I get the admin page to automatically display the changes to those fields as happens with the pre-installed delete admin action once the action is executed? I have experimented with using a HttpResponseRedirect as a work around but have not been able to get that to work in conjunction with my reponse object - only one or the other work. Is there a simple method for getting the page to auto refresh? Thanks in advance for any guidance!
from django.contrib import admin
from django.contrib.auth.models import User
from djangostuff.pdf.models import ABC
from django.http import HttpResponse, HttpResponseRedirect
import datetime, time
class ABCAdmin(admin.ModelAdmin):
actions = ['print_selected_pdf']
def get_user(self):
return '%s'%(self.user.username)
def create_pdf(self, request, queryset):
response = HttpResponse(mimetype="application/pdf")
response['Content-Disposition'] = 'attachment; filename=form.pdf'
for obj in queryset:
response.write(obj.form)
rows_updated = ABC.objects.filter(pk=obj.pk).update(user=request.user,pdf_printed="1",request_time=time.strftime("%H:%M:%S"),request_date=datetime.datetime.today())
if rows_updated == 1:
开发者_运维知识库 message_bit = "1 form was"
else:
message_bit = "%s forms were" % rows_updated
self.message_user(request, "%s successfully printed." % message_bit)
return response
#HttpResponseRedirect("/admin/pdf/abc")
def print_selected_pdf(self, request, queryset):
# prints the pdfs for those that are selected,
# regardless if the pdf_printed field is true or false
qs = queryset.filter(pdf_printed__exact=0)
return self.create_pdf(request, qs)
print_selected_pdf.short_description = "Print Selected PDF"
get_user.short_description='Printed By'
list_display=('form_no',get_user,'request_date','request_time','pdf_printed')
admin.site.register(ABC, ABCAdmin)
To solve this issue, you can create an intermediary page to redirect for confirmation, then redirect the user back to the original change list page and your message will be displayed. The built in admin action for deleting an object already accomplishes this.
You can read more about it here:
https://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/#actions-that-provide-intermediate-pages
精彩评论