MCPcopy
hub / github.com/django/django / delete_selected

Function delete_selected

django/contrib/admin/actions.py:19–96  ·  view source on GitHub ↗

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 obje

(modeladmin, request, queryset)

Source from the content-addressed store, hash-verified

17 description=gettext_lazy("Delete selected %(verbose_name_plural)s"),
18)
19def delete_selected(modeladmin, request, queryset):
20 """
21 Default action which deletes the selected objects.
22
23 This action first displays a confirmation page which shows all the
24 deletable objects, or, if the user has no permission one of the related
25 childs (foreignkeys), a "permission denied" message.
26
27 Next, it deletes all selected objects and redirects back to the change
28 list.
29 """
30 opts = modeladmin.model._meta
31 app_label = opts.app_label
32
33 # Populate deletable_objects, a data structure of all related objects that
34 # will also be deleted.
35 (
36 deletable_objects,
37 model_count,
38 perms_needed,
39 protected,
40 ) = modeladmin.get_deleted_objects(queryset, request)
41
42 # The user has already confirmed the deletion.
43 # Do the deletion and return None to display the change list view again.
44 if request.POST.get("post") and not protected:
45 if perms_needed:
46 raise PermissionDenied
47 n = len(queryset)
48 if n:
49 modeladmin.log_deletions(request, queryset)
50 modeladmin.delete_queryset(request, queryset)
51 modeladmin.message_user(
52 request,
53 _("Successfully deleted %(count)d %(items)s.")
54 % {"count": n, "items": model_ngettext(modeladmin.opts, n)},
55 messages.SUCCESS,
56 )
57 # Return None to display the change list page again.
58 return None
59
60 objects_name = model_ngettext(queryset)
61
62 if perms_needed or protected:
63 title = _("Cannot delete %(name)s") % {"name": objects_name}
64 else:
65 title = _("Delete multiple objects")
66
67 context = {
68 **modeladmin.admin_site.each_context(request),
69 "title": title,
70 "subtitle": None,
71 "objects_name": str(objects_name),
72 "deletable_objects": [deletable_objects],
73 "delete_confirmation_max_display": modeladmin.delete_confirmation_max_display,
74 "model_count": dict(model_count).items(),
75 "queryset": queryset,
76 "perms_lacking": perms_needed,

Callers

nothing calls this directly

Calls 9

model_ngettextFunction · 0.90
TemplateResponseClass · 0.90
log_deletionsMethod · 0.80
message_userMethod · 0.80
each_contextMethod · 0.80
get_deleted_objectsMethod · 0.45
getMethod · 0.45
delete_querysetMethod · 0.45
itemsMethod · 0.45

Tested by

no test coverage detected