﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
16913	Admin error when trying to delete object with a GenericRelation	Ricky Rosario	Carl Meyer	"If you have a model A with a GenericForeignKey and a model B with a GenericRelation to A:
{{{
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.db import models


class ModelA(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')


class ModelB(models.Model):
    model_a = generic.GenericRelation(ModelA, related_name='my_related')
}}}


Create a ModelA and a related Model B:
{{{
b = ModelB.objects.create()
a = b.model_a.create()
}}}

Then try to delete B in the admin and you get a 500:
{{{

Traceback:
File ""/Users/rlr/.virtualenvs/django/lib/python2.6/site-packages/django/core/handlers/base.py"" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File ""/Users/rlr/.virtualenvs/django/lib/python2.6/site-packages/django/contrib/admin/options.py"" in wrapper
  328.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File ""/Users/rlr/.virtualenvs/django/lib/python2.6/site-packages/django/utils/decorators.py"" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File ""/Users/rlr/.virtualenvs/django/lib/python2.6/site-packages/django/views/decorators/cache.py"" in _wrapped_view_func
  88.         response = view_func(request, *args, **kwargs)
File ""/Users/rlr/.virtualenvs/django/lib/python2.6/site-packages/django/contrib/admin/sites.py"" in inner
  192.             return view(request, *args, **kwargs)
File ""/Users/rlr/.virtualenvs/django/lib/python2.6/site-packages/django/utils/decorators.py"" in _wrapper
  25.             return bound_func(*args, **kwargs)
File ""/Users/rlr/.virtualenvs/django/lib/python2.6/site-packages/django/utils/decorators.py"" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File ""/Users/rlr/.virtualenvs/django/lib/python2.6/site-packages/django/utils/decorators.py"" in bound_func
  21.                 return func(self, *args2, **kwargs2)
File ""/Users/rlr/.virtualenvs/django/lib/python2.6/site-packages/django/db/transaction.py"" in inner
  209.                 return func(*args, **kwargs)
File ""/Users/rlr/.virtualenvs/django/lib/python2.6/site-packages/django/contrib/admin/options.py"" in delete_view
  1255.             [obj], opts, request.user, self.admin_site, using)
File ""/Users/rlr/.virtualenvs/django/lib/python2.6/site-packages/django/contrib/admin/util.py"" in get_deleted_objects
  76.     collector.collect(objs)
File ""/Users/rlr/.virtualenvs/django/lib/python2.6/site-packages/django/contrib/admin/util.py"" in collect
  127.             return super(NestedObjects, self).collect(objs, source_attr=source_attr, **kwargs)
File ""/Users/rlr/.virtualenvs/django/lib/python2.6/site-packages/django/db/models/deletion.py"" in collect
  180.                                  nullable=True)
File ""/Users/rlr/.virtualenvs/django/lib/python2.6/site-packages/django/contrib/admin/util.py"" in collect
  123.                 self.add_edge(getattr(obj, source_attr), obj)

Exception Type: AttributeError at /admin/testapp/modelb/1/delete/
Exception Value: 'ModelA' object has no attribute 'my_related'
}}}


It looks like Generic Relations don't add the reverse manager to the related model (not sure if that is a bug or a feauture?) but the NestedObjects collector assumes that it will be there.

I added created a test that reproduces the issue and a potential fix: https://github.com/rlr/django/commit/8d147677fc"	Bug	closed	contrib.admin	1.3	Normal	duplicate		James Socol	Accepted	1	0	0	0	0	0
