Find deletable objects

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
""" Find deletable of objects"""
from django.conf import settings
from django.db.utils import ConnectionRouter
from django.contrib.admin.util import get_deleted_objects
from django.contrib.auth.models import User
from django.contrib.admin import AdminSite


def find_deletable_objects(queryset):
    """ Get a full list of deletable objects. """
    router = ConnectionRouter(settings.DATABASE_ROUTERS)
    modeladmin = AccountAdmin
    modeladmin.model = Account
    modeladmin.admin_site = AdminSite()
    using = router.db_for_write(modeladmin.model)
    opts = modeladmin.model._meta 
    user = User.objects.get_or_create(id=1)  # superuser
    deletable_objects, perms_needed, protected = get_deleted_objects(
        queryset, opts, user, modeladmin.admin_site, using)

    from pprint import pprint
    pprint(deletable_objects)

More like this

  1. Delete View by alcides 3 years, 9 months ago
  2. Add delete buttons to admin changelist by kylefox 4 years, 12 months ago
  3. REMOVE IMAGEFIELD ATTACHMENT IN DJANGO by timonweb 2 weeks, 1 day ago
  4. Owner required decorator by polarbear 3 years, 10 months ago
  5. Add delete button in admin cp by cschand 4 years, 1 month ago

Comments

(Forgotten your password?)