Code

Ticket #5642: patch-foreignkey-attributeerror.diff

File patch-foreignkey-attributeerror.diff, 6.9 KB (added by Gregor_Kopka, 7 years ago)
Line 
1Index: django/db/models/options.py
2===================================================================
3--- django/db/models/options.py (revision 6436)
4+++ django/db/models/options.py (working copy)
5@@ -152,7 +152,7 @@
6             rel_objs = []
7             for klass in get_models():
8                 for f in klass._meta.fields:
9-                    if f.rel and self == f.rel.to._meta:
10+                    if f.rel and not isinstance(f.rel.to, str) and self == f.rel.to._meta:
11                         rel_objs.append(RelatedObject(f.rel.to, klass, f))
12             self._all_related_objects = rel_objs
13             return rel_objs
14@@ -186,7 +186,7 @@
15             rel_objs = []
16             for klass in get_models():
17                 for f in klass._meta.many_to_many:
18-                    if f.rel and self == f.rel.to._meta:
19+                    if f.rel and not isinstance(f.rel.to, str) and self == f.rel.to._meta:
20                         rel_objs.append(RelatedObject(f.rel.to, klass, f))
21             if app_cache_ready():
22                 self._all_related_many_to_many_objects = rel_objs
23Index: django/core/management/validation.py
24===================================================================
25--- django/core/management/validation.py        (revision 6436)
26+++ django/core/management/validation.py        (working copy)
27@@ -70,33 +70,33 @@
28             # Check to see if the related field will clash with any
29             # existing fields, m2m fields, m2m related objects or related objects
30             if f.rel:
31-                rel_opts = f.rel.to._meta
32                 if f.rel.to not in models.get_models():
33                     e.add(opts, "'%s' has relation with model %s, which has not been installed" % (f.name, rel_opts.object_name))
34-
35-                rel_name = RelatedObject(f.rel.to, cls, f).get_accessor_name()
36-                rel_query_name = f.related_query_name()
37-                for r in rel_opts.fields:
38-                    if r.name == rel_name:
39-                        e.add(opts, "Accessor for field '%s' clashes with field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.name, f.name))
40-                    if r.name == rel_query_name:
41-                        e.add(opts, "Reverse query name for field '%s' clashes with field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.name, f.name))
42-                for r in rel_opts.many_to_many:
43-                    if r.name == rel_name:
44-                        e.add(opts, "Accessor for field '%s' clashes with m2m field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.name, f.name))
45-                    if r.name == rel_query_name:
46-                        e.add(opts, "Reverse query name for field '%s' clashes with m2m field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.name, f.name))
47-                for r in rel_opts.get_all_related_many_to_many_objects():
48-                    if r.get_accessor_name() == rel_name:
49-                        e.add(opts, "Accessor for field '%s' clashes with related m2m field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.get_accessor_name(), f.name))
50-                    if r.get_accessor_name() == rel_query_name:
51-                        e.add(opts, "Reverse query name for field '%s' clashes with related m2m field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.get_accessor_name(), f.name))
52-                for r in rel_opts.get_all_related_objects():
53-                    if r.field is not f:
54+                else:
55+                    rel_opts = f.rel.to._meta
56+                    rel_name = RelatedObject(f.rel.to, cls, f).get_accessor_name()
57+                    rel_query_name = f.related_query_name()
58+                    for r in rel_opts.fields:
59+                        if r.name == rel_name:
60+                            e.add(opts, "Accessor for field '%s' clashes with field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.name, f.name))
61+                        if r.name == rel_query_name:
62+                            e.add(opts, "Reverse query name for field '%s' clashes with field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.name, f.name))
63+                    for r in rel_opts.many_to_many:
64+                        if r.name == rel_name:
65+                            e.add(opts, "Accessor for field '%s' clashes with m2m field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.name, f.name))
66+                        if r.name == rel_query_name:
67+                            e.add(opts, "Reverse query name for field '%s' clashes with m2m field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.name, f.name))
68+                    for r in rel_opts.get_all_related_many_to_many_objects():
69                         if r.get_accessor_name() == rel_name:
70-                            e.add(opts, "Accessor for field '%s' clashes with related field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.get_accessor_name(), f.name))
71+                            e.add(opts, "Accessor for field '%s' clashes with related m2m field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.get_accessor_name(), f.name))
72                         if r.get_accessor_name() == rel_query_name:
73-                            e.add(opts, "Reverse query name for field '%s' clashes with related field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.get_accessor_name(), f.name))
74+                            e.add(opts, "Reverse query name for field '%s' clashes with related m2m field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.get_accessor_name(), f.name))
75+                    for r in rel_opts.get_all_related_objects():
76+                        if r.field is not f:
77+                            if r.get_accessor_name() == rel_name:
78+                                e.add(opts, "Accessor for field '%s' clashes with related field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.get_accessor_name(), f.name))
79+                            if r.get_accessor_name() == rel_query_name:
80+                                e.add(opts, "Reverse query name for field '%s' clashes with related field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.get_accessor_name(), f.name))
81 
82         for i, f in enumerate(opts.many_to_many):
83             # Check to see if the related m2m field will clash with any