Django 管理操作不重复
Django admin action not repeating
我有以下管理操作(在我的 admin.py 文件中),旨在下载所选项目的 pdf 文件。除了它只会为查询集中的第一项创建和下载 pdf 之外,它似乎还在工作。我认为问题出在 'return response' 行,但我不知道还能用什么代替它。任何输入都会很棒,我被踩死了!
@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')
在return之后,循环(和print_release
方法)已经退出。这显然是一个错误。您希望在函数结束时 return,而不是中途。
我不会为您编写程序,但显然您需要在循环中间累积部分结果,而不是 returning,然后在最后 return 累积结果。
我有以下管理操作(在我的 admin.py 文件中),旨在下载所选项目的 pdf 文件。除了它只会为查询集中的第一项创建和下载 pdf 之外,它似乎还在工作。我认为问题出在 'return response' 行,但我不知道还能用什么代替它。任何输入都会很棒,我被踩死了!
@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')
在return之后,循环(和print_release
方法)已经退出。这显然是一个错误。您希望在函数结束时 return,而不是中途。
我不会为您编写程序,但显然您需要在循环中间累积部分结果,而不是 returning,然后在最后 return 累积结果。