我一直在调整这篇文章How to Add Custom Action Buttons to Django Admin以适应我的特殊情况,除了我得到的这个奇怪错误之外,事情都很顺利。在forms.py
我已定义class DenyForm
:(注意request
中的form_action
参数不是通常的API请求。我有一个模型class Request
。)应该form_action回来吗?
class DenyForm(ApproveOrDenyForm):
def form_action(self, request, admin_approver ):
print "forms DenyForm Called."
justification = self.cleaned_data['justification']
request.admin_deny(admin_approver=admin_approver,justification=justification)
return [request]
#return request.admin_deny(admin_approver=admin_approver,justification=justification)
这会产生
的消息“‘Please correct the errors below.’ need more than 1 value to unpack”.
我尝试过不同的form_action方法。如果我的最后一行是:
return request
我得到“‘Please correct the errors below.’ ‘Request’ object is not iterable”.
如果最后一行是:
return request.admin_deny(admin_approver=admin_approver,justification=justification)
我明白了...... “‘Please correct the errors below.’ ‘NoneType’ object is not iterable”
。那是因为admin_deny()
没有返回任何东西。 form_action
应该返回什么?
更新:以下是调用form_action()
的代码:
class ApproveOrDenyForm(forms.Form):
justification = forms.CharField(
required=True,
widget=forms.Textarea,
)
def save(self, req, login ):
try:
user = User.objects.filter(login=login).get()
req, action = self.form_action(req, user )
except Exception as e:
error_message = str(e)
self.add_error(None, error_message)
raise
return req, action
答案 0 :(得分:1)
当您致电form_action
时,您希望它能够返回两个项目的可迭代项req
和action
。因此,您需要确保form_action()
返回正是这两件事的列表或元组。
我不清楚你链接到的帖子或你的代码,action
返回的内容应该是什么 - 可能只是执行操作的结果。您需要检查以确定您的操作方法是否需要返回其他内容。
这样的事情应该有效:
def form_action(self, request, admin_approver ):
justification = self.cleaned_data['justification']
action = request.admin_deny(admin_approver=admin_approver, justification=justification)
# action will be None if admin_deny does not return anything,
# but the code calling this function expects it, so return it anyway.
return (request, action)