Code

Ticket #2199: related.diff

File related.diff, 3.0 KB (added by parlar@…, 8 years ago)

[patch] Corrects the error

Line 
1Index: contrib/admin/views/main.py
2===================================================================
3--- contrib/admin/views/main.py (revision 3145)
4+++ contrib/admin/views/main.py (working copy)
5@@ -112,6 +112,7 @@
6     def __init__(self, field, field_mapping, original):
7         self.field = field
8         self.original = original
9+        print "NAMES", list(self.field.get_manipulator_field_names(''))
10         self.form_fields = [field_mapping[name] for name in self.field.get_manipulator_field_names('')]
11         self.element_id = self.form_fields[0].get_id()
12         self.has_label_first = not isinstance(self.field, models.BooleanField)
13@@ -462,19 +463,23 @@
14         opts_seen.append(related.opts)
15         rel_opts_name = related.get_accessor_name()
16         has_related_objs = False
17-        for sub_obj in getattr(obj, rel_opts_name).all():
18+        rel_objs = getattr(obj, rel_opts_name, None)
19+        if rel_objs:
20             has_related_objs = True
21-            if related.field.rel.edit_inline or not related.opts.admin:
22-                # Don't display link to edit, because it either has no
23-                # admin or is edited inline.
24-                nh(deleted_objects, current_depth, [_('One or more %(fieldname)s in %(name)s: %(obj)s') % \
25-                    {'fieldname': related.field.verbose_name, 'name': related.opts.verbose_name, 'obj': escape(str(sub_obj))}, []])
26-            else:
27-                # Display a link to the admin page.
28-                nh(deleted_objects, current_depth, [
29-                    (_('One or more %(fieldname)s in %(name)s:') % {'fieldname': related.field.verbose_name, 'name':related.opts.verbose_name}) + \
30-                    (' <a href="../../../../%s/%s/%s/">%s</a>' % \
31-                        (related.opts.app_label, related.opts.module_name, sub_obj._get_pk_val(), escape(str(sub_obj)))), []])
32+
33+        if has_related_objs:
34+            for sub_obj in rel_objs.all():
35+                if related.field.rel.edit_inline or not related.opts.admin:
36+                    # Don't display link to edit, because it either has no
37+                    # admin or is edited inline.
38+                    nh(deleted_objects, current_depth, [_('One or more %(fieldname)s in %(name)s: %(obj)s') % \
39+                        {'fieldname': related.field.verbose_name, 'name': related.opts.verbose_name, 'obj': escape(str(sub_obj))}, []])
40+                else:
41+                    # Display a link to the admin page.
42+                    nh(deleted_objects, current_depth, [
43+                        (_('One or more %(fieldname)s in %(name)s:') % {'fieldname': related.field.verbose_name, 'name':related.opts.verbose_name}) + \
44+                        (' <a href="../../../../%s/%s/%s/">%s</a>' % \
45+                            (related.opts.app_label, related.opts.module_name, sub_obj._get_pk_val(), escape(str(sub_obj)))), []])
46         # If there were related objects, and the user doesn't have
47         # permission to change them, add the missing perm to perms_needed.
48         if related.opts.admin and has_related_objs: