I have the following Admin action (in my admin.py file) which in designed to download pdf files for selected items. It seems to be working apart from the fact that it will only create and download a pdf for the first item in the queryset. I think the problem lies in the 'return response' line but I don't know what else to use in its place. Any input would be great, I'm stomped!
@admin.register(ReleaseForm)
class ReleaseAdmin(admin.ModelAdmin):
def participant(self, obj):
return str(obj.customer.last_name) + ", " + str(obj.customer.first_name)
def training(self, obj):
return str(obj.order.training_registered.name)
def print_release(self, request, queryset):
updated=queryset.count()
print (updated)
for obj in queryset.all():
customer=obj.customer
order=Order.objects.get(customer=customer)
firstname = obj.customer.first_name
lastname = obj.customer.last_name
nwta = order.training_registered.name
data = {'order':order,'firstname': firstname, 'lastname': lastname, 'nwta':nwta,}
pdf=release_render_to_pdf('accounts/pdf_template.html', data)
response = HttpResponse(pdf, content_type='application/pdf')
filename = "Release_%s_%s.pdf" %(lastname,nwta,)
content="attachment; filename=%s" %(filename)
response['Content-Disposition'] = content
print(obj.customer)
return response
self.message_user(request, ngettext(
'%d Relase Form was successfully printed.',
'%d Relase Forms were successfully printed.',
updated,
) % updated, messages.SUCCESS)
print_release.short_description="Print Release Form(s)"
list_display = ('participant','release_status','release_date_submitted' ,'note' )
actions = ['print_release']
ordering = ('customer',)
list_filter = ('customer__order__training_registered__training__name','customer__order__training_registered', 'customer__order__regstatus','release_status','release_date_submitted' ,'note')
search_fields = ('participant','note')
After the return, the loop (and the print_release
method) has been exited. This is clearly an error. You want to return at the end of the function, not part-way through.
I am not going to write your program for you, but clearly instead of returning in the middle of the loop you need to accumulate partial results, then at the end return the accumulated result.