Skip to content

Instantly share code, notes, and snippets.

@epicserve
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save epicserve/eafce65f1426557e6a05 to your computer and use it in GitHub Desktop.
Save epicserve/eafce65f1426557e6a05 to your computer and use it in GitHub Desktop.
A handy little mixin for adding related objects to a view like DeleteView.
from django.views.generic.base import ContextMixin
from django.contrib.admin.utils import NestedObjects
class RelatedObjectsMixin(ContextMixin):
"""Adds the related_objects list to a views context """
def get_context_data(self, **kwargs):
context = super(RelatedObjectsMixin, self).get_context_data(**kwargs)
collector = NestedObjects(using='default')
collector.collect([context['object']])
to_delete = collector.nested()
context['related_objects'] = to_delete
return context
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment