我正在考虑在管理员之外创建类似管理操作的行为。因此,用户应该能够使用复选框选择对象,并且在使用下拉列表选择操作后,将对所有选定的对象执行所选操作。
我想到了以下方法。
为 .html 模板中的每个对象创建一个复选框。结果是这样的(我从管理员那里得到的):
<td><input type="checkbox" class="action-select" value="24" name="_selected_action" /></td>
然后创建操作下拉列表(也从管理员处获取):
<div class="actions">
<label>Action: <select name="action">
<option value="" selected="selected">---------</option>
<option value="delete_selected">Delete selected contacts</option>
</select></label>
<button type="submit" class="button" title="Run the selected action" name="index" value="0">Go</button>
</div>
现在我问自己如何将这个操作下拉列表映射到 python 函数。可能是我将放置在我想要应用此操作的模型对象内的函数。
我希望有人能告诉我。
下一点是我如何在此功能中检查已选择哪些对象。
当我发现它是如何工作的时,我可以使用这个对象并返回一个 HttpResponse。
我认为这应该是创造类似行为的一切。这是正确的还是缺少什么?
编辑:我的解决方案
最后我想出了以下解决方案:
我创建了一个名为 action_handler 的函数,它接受请求和它将执行的模型。从帖子查询字典中,我获得了选定的操作(request.POST.getitem('action')) 和选定的对象 (request.POST.getlist('_selected_for_action'))。
根据输入和过程,函数返回一些值,这些值告诉调用视图在action_handler 中发生了什么。
@login_required
def action_handler(request, model):
"""
PARAMETERS
model = the django model upon which the actions are executed
RETURN VALUES
0 - POST param action is not available
- POST param selected_action is not defined
- selected_ids is empty
- object_list is empty
1 - Successfully conducted the delete_selected action
2 - Successfully conducted the new_selection action
DESCRIPTION
handles all actions for arbitrary models
Action:
"delete selected" - calls the generic delete method, delete_objects(request, model, selected_ids)
"""
try:
selected_action = request.POST.__getitem__('action')
selected_ids = request.POST.getlist('_selected_for_action')
object_list = model.objects.filter(pk__in=selected_ids)
if object_list.count() < 1:
request.user.message_set.create(message='Please select at least one item!')
return 0
if selected_action == 'delete_selected':
try:
action_approved = request.POST.__getitem__('action_approved')
if action_approved == '1':
delete_objects(request, model, selected_ids)
return 1
except KeyError:
#action_approved param is not available
#show the objects check page for delete approval
context = {
'action_name' : selected_action,
'object_list' : object_list,
}
return render_to_response("crm/object_delete_check.html", context,
context_instance=RequestContext(request))
if selected_action == 'new_selection':
#add the selected objects to a new cs selection
now = datetime.now()
stamp = now.strftime("%Y-%m-%d/%H:%M")
cs_name = 'cs_auto_created_' + str(stamp)
cs = ContactSelection(id=None, name=cs_name)
cs.created_by = request.user
cs.save()
for object in object_list:
cs.contacts.add(object)
request.user.message_set.create(message='Successfully created the %s selection' % cs.name)
return 2
request.user.message_set.create(message='This action is not available!')
return 0
except KeyError:
request.user.message_set.create(message='Please select an action!')
return 0
删除delete_objects(request, model, selected_ids)看起来像这样:
@login_required
def delete_objects(request, model, selected_ids):
'''
capsulate a bulk delete method
delete all objects found for the given model
fails silently since model.delete() always fails silently
'''
object_list = model.objects.filter(pk__in=selected_ids)
count = object_list.count()
if count == 1:
name = model._meta.verbose_name.title()
else:
name = model._meta.verbose_name_plural.title()
object_list.delete()
request.user.message_set.create(message='Successfully deleted %s %s' % (count,name))
return
这样我就可以为不同的视图包含这个action_handler,并封装一个独立于该函数所作用的模型的删除函数。