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. Lightweight querysets by sardarnl 2 days, 5 hours ago
  2. SelectRelatedManager by realmac 1 year, 9 months ago
  3. Admin list_display Ajax by whiteinge 5 years, 4 months ago
  4. self-related objects list with links by kumbry 5 years, 7 months ago
  5. Easier chainability with custom QuerySets by bendavis78 1 year, 3 months ago

Comments

(Forgotten your password?)