def delete_selected(modeladmin, request, queryset): """ Default action which deletes the selected objects. This action first displays a confirmation page which shows all the deletable objects, or, if the user has no permission one of the related childs (foreignkeys), a "permission denied" message. Next, it deletes all selected objects and redirects back to the change list. """ opts = modeladmin.model._meta app_label = opts.app_label # Check that the user has delete permission for the actual model if not modeladmin.has_delete_permission(request): raise PermissionDenied using = router.db_for_write(modeladmin.model) # Populate deletable_objects, a data structure of all related objects that # will also be deleted. deletable_objects, model_count, perms_needed, protected = get_deleted_objects( queryset, opts, request.user, modeladmin.admin_site, using) # The user has already confirmed the deletion. # Do the deletion and return a None to display the change list view again. if request.POST.get('post') and not protected: if perms_needed: raise PermissionDenied n = queryset.count() if n: for obj in queryset: obj_display = force_text(obj) modeladmin.log_deletion(request, obj, obj_display) queryset.delete() modeladmin.message_user(request, _("Successfully deleted %(count)d %(items)s.") % { "count": n, "items": model_ngettext(modeladmin.opts, n) }, messages.SUCCESS) # Return None to display the change list page again. return None if len(queryset) == 1: objects_name = force_text(opts.verbose_name) else: objects_name = force_text(opts.verbose_name_plural) if perms_needed or protected: title = _("Cannot delete %(name)s") % {"name": objects_name} else: title = _("Are you sure?") context = dict( modeladmin.admin_site.each_context(request), title=title, objects_name=objects_name, deletable_objects=[deletable_objects], model_count=dict(model_count).items(), queryset=queryset, perms_lacking=perms_needed, protected=protected, opts=opts, action_checkbox_name=helpers.ACTION_CHECKBOX_NAME, media=modeladmin.media, ) request.current_app = modeladmin.admin_site.name # Display the confirmation page return TemplateResponse(request, modeladmin.delete_selected_confirmation_template or [ "admin/%s/%s/delete_selected_confirmation.html" % (app_label, opts.model_name), "admin/%s/delete_selected_confirmation.html" % app_label, "admin/delete_selected_confirmation.html" ], context) delete_selected.short_description = ugettext_lazy("Delete selected %(verbose_name_plural)s")
时间: 2024-02-14 12:07:01 浏览: 242
浅谈Django QuerySet对象(模型.objects)的常用方法
这段代码定义了一个默认的 Django Admin 后台操作,用于批量删除选中的对象。具体来说,这个操作会首先显示一个确认页面,列出所有将被删除的对象,以及与这些对象相关的其他对象(例如外键关联的对象)。如果用户没有删除权限,或者某些对象受到保护,则会显示一个“无权删除”或“不能删除”的消息。如果用户确认删除操作,则会删除所有选中的对象,并重定向到对象列表页面。
在代码中,`delete_selected()` 函数接受三个参数:`modeladmin` 表示 Django admin 后台的管理器对象,`request` 表示当前的 HTTP 请求对象,`queryset` 表示选中的对象的 QuerySet。函数首先获取模型的元数据信息,然后检查当前用户是否有删除权限。如果没有权限,则会引发 `PermissionDenied` 异常。接下来,函数调用 `get_deleted_objects()` 方法获取所有将被删除的对象及相关对象的信息,并根据用户的选择执行删除操作或者显示确认页面。最后,函数返回一个 `TemplateResponse` 对象,用于显示确认页面或者执行删除操作后的重定向。此外,函数还定义了一个 `short_description` 属性,用于在 Django admin 后台中显示操作名称。
阅读全文