Login

Find deletable objects

Author:
writefaruq
Posted:
June 23, 2011
Language:
Python
Version:
1.3
Tags:
admin deletable_objects
Score:
0 (after 0 ratings)

In case when you need a list of related objects for particular queryset

 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. serialize model object with related objects to dict by zakovinko 4 years, 1 month ago
  2. Callable object as signal receiver by diverman 5 years, 12 months ago
  3. really_equals by zeeg 7 years, 10 months ago
  4. Hat-trick for getting categories related to an object group by pablo_PXL 4 years, 1 month ago
  5. Simple Signal to denormalize vote counts in django-voting by ericmoritz 6 years, 6 months ago

Comments

Please login first before commenting.