- migrate code to use collector directly instead of the NestedObjects wrapper from admin.utils

- adjust object names and text output
This commit is contained in:
Jamie Murphy 2023-10-20 07:52:06 +01:00
parent 7c3caf5e39
commit 63b1736c97
No known key found for this signature in database
GPG Key ID: 10EDAE1E6D858F5F
2 changed files with 14 additions and 11 deletions

View File

@ -2,9 +2,9 @@ import logging
from copy import deepcopy
from django.contrib import messages
from django.contrib.admin.utils import NestedObjects
from django.db import transaction, router
from django.db.models import ProtectedError
from django.db.models.deletion import Collector
from django.shortcuts import redirect, render
from django.urls import reverse
from django.utils.html import escape
@ -336,13 +336,16 @@ class ObjectDeleteView(GetReturnURLMixin, BaseObjectView):
form = ConfirmationForm(initial=request.GET)
using = router.db_for_write(obj._meta.model)
collector = NestedObjects(using=using)
collector = Collector(using=using)
collector.collect([obj])
nested_objs = []
deletion_objects = []
if collector.instances_with_model():
for model, instance in collector.instances_with_model():
nested_objs.append(f"{model.__name__} - {instance}")
# we could ignore the instance == obj so that the list doesnt contain itself...
deletion_objects.append({
"modelname":f"{model.__name__}",
"object": instance,
})
# If this is an HTMX request, return only the rendered deletion form as modal content
if is_htmx(request):
@ -353,7 +356,7 @@ class ObjectDeleteView(GetReturnURLMixin, BaseObjectView):
'object_type': self.queryset.model._meta.verbose_name,
'form': form,
'form_url': form_url,
'nested_objs': nested_objs,
'deletion_objects': deletion_objects,
**self.get_extra_context(request, obj),
})
@ -361,7 +364,7 @@ class ObjectDeleteView(GetReturnURLMixin, BaseObjectView):
'object': obj,
'form': form,
'return_url': self.get_return_url(request, obj),
'nested_objs': nested_objs,
'deletion_objects': deletion_objects,
**self.get_extra_context(request, obj),
})

View File

@ -12,11 +12,11 @@
Are you sure you want to <strong class="text-danger">delete</strong> {{ object_type }} <strong>{{ object }}</strong>?
{% endblocktrans %}
</p>
{% if nested_objs %}
{% if deletion_objects %}
<p>
This will cause deltion of the following related items: <br>
{% for nested_obj in nested_objs %}
- {{ nested_obj }} <br>
This will cause deltion of the following items: <br>
{% for deletion_object in deletion_objects %}
- {{ deletion_object.modelname }} - {{ deletion_object.object | linkify }} <br>
{% endfor %}
</p>
{% endif %}