From bc2d8cdbc6d923dedc153949fb3a97e0cdd0c43d Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Thu, 22 Jun 2006 18:44:06 +0000 Subject: [PATCH] Fixed #2199: deleting objects with generic foreign keys in the admin now works. Thanks, Jay Parlar git-svn-id: http://code.djangoproject.com/svn/django/trunk@3194 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/admin/views/main.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/django/contrib/admin/views/main.py b/django/contrib/admin/views/main.py index ea7aeb490b..a7ee08f1c0 100644 --- a/django/contrib/admin/views/main.py +++ b/django/contrib/admin/views/main.py @@ -462,19 +462,23 @@ def _get_deleted_objects(deleted_objects, perms_needed, user, obj, opts, current opts_seen.append(related.opts) rel_opts_name = related.get_accessor_name() has_related_objs = False - for sub_obj in getattr(obj, rel_opts_name).all(): + rel_objs = getattr(obj, rel_opts_name, None) + if rel_objs: has_related_objs = True - if related.field.rel.edit_inline or not related.opts.admin: - # Don't display link to edit, because it either has no - # admin or is edited inline. - nh(deleted_objects, current_depth, [_('One or more %(fieldname)s in %(name)s: %(obj)s') % \ - {'fieldname': related.field.verbose_name, 'name': related.opts.verbose_name, 'obj': escape(str(sub_obj))}, []]) - else: - # Display a link to the admin page. - nh(deleted_objects, current_depth, [ - (_('One or more %(fieldname)s in %(name)s:') % {'fieldname': related.field.verbose_name, 'name':related.opts.verbose_name}) + \ - (' %s' % \ - (related.opts.app_label, related.opts.module_name, sub_obj._get_pk_val(), escape(str(sub_obj)))), []]) + + if has_related_objs: + for sub_obj in rel_objs.all(): + if related.field.rel.edit_inline or not related.opts.admin: + # Don't display link to edit, because it either has no + # admin or is edited inline. + nh(deleted_objects, current_depth, [_('One or more %(fieldname)s in %(name)s: %(obj)s') % \ + {'fieldname': related.field.verbose_name, 'name': related.opts.verbose_name, 'obj': escape(str(sub_obj))}, []]) + else: + # Display a link to the admin page. + nh(deleted_objects, current_depth, [ + (_('One or more %(fieldname)s in %(name)s:') % {'fieldname': related.field.verbose_name, 'name':related.opts.verbose_name}) + \ + (' %s' % \ + (related.opts.app_label, related.opts.module_name, sub_obj._get_pk_val(), escape(str(sub_obj)))), []]) # If there were related objects, and the user doesn't have # permission to change them, add the missing perm to perms_needed. if related.opts.admin and has_related_objs: