Code

Ticket #15669: ticket15669_2.diff

File ticket15669_2.diff, 8.9 KB (added by Jure Cuhalev <gandalf@…>, 3 years ago)

Updated patch so that modeladmin tests also pass. I also tried approach suggested by Alex and abandoned it because unicode() repr of model lowercases the model, making it harder to grep for errors.

Line 
1diff --git a/django/contrib/admin/validation.py b/django/contrib/admin/validation.py
2index 58a8490..9a0c948 100644
3--- a/django/contrib/admin/validation.py
4+++ b/django/contrib/admin/validation.py
5@@ -104,8 +104,8 @@ def validate(cls, model):
6                 field = opts.get_field_by_name(field_name)[0]
7             except models.FieldDoesNotExist:
8                 raise ImproperlyConfigured("'%s.list_editable[%d]' refers to a "
9-                    "field, '%s', not defined on %s."
10-                    % (cls.__name__, idx, field_name, model.__name__))
11+                    "field, '%s', not defined on %s.%s."
12+                    % (cls.__name__, idx, field_name, model._meta.app_label, model.__name__))
13             if field_name not in cls.list_display:
14                 raise ImproperlyConfigured("'%s.list_editable[%d]' refers to "
15                     "'%s' which is not defined in 'list_display'."
16@@ -215,7 +215,7 @@ def validate_inline(cls, parent, parent_model):
17         if fk and fk.name in cls.exclude:
18             raise ImproperlyConfigured("%s cannot exclude the field "
19                     "'%s' - this is the foreign key to the parent model "
20-                    "%s." % (cls.__name__, fk.name, parent_model.__name__))
21+                    "%s.%s." % (cls.__name__, fk.name, parent_model._meta.app_label, parent_model.__name__))
22 
23     if hasattr(cls, "readonly_fields"):
24         check_readonly_fields(cls, cls.model, cls.model._meta)
25@@ -372,8 +372,8 @@ def get_field(cls, model, opts, label, field):
26     try:
27         return opts.get_field(field)
28     except models.FieldDoesNotExist:
29-        raise ImproperlyConfigured("'%s.%s' refers to field '%s' that is missing from model '%s'."
30-                % (cls.__name__, label, field, model.__name__))
31+        raise ImproperlyConfigured("'%s.%s' refers to field '%s' that is missing from model '%s.%s'."
32+                % (cls.__name__, label, field, model._meta.app_label, model.__name__))
33 
34 def check_formfield(cls, model, opts, label, field):
35     if getattr(cls.form, 'base_fields', None):
36@@ -398,8 +398,8 @@ def fetch_attr(cls, model, opts, label, field):
37     try:
38         return getattr(model, field)
39     except AttributeError:
40-        raise ImproperlyConfigured("'%s.%s' refers to '%s' that is neither a field, method or property of model '%s'."
41-            % (cls.__name__, label, field, model.__name__))
42+        raise ImproperlyConfigured("'%s.%s' refers to '%s' that is neither a field, method or property of model '%s.%s'."
43+            % (cls.__name__, label, field, model._meta.app_label, model.__name__))
44 
45 def check_readonly_fields(cls, model, opts):
46     check_isseq(cls, "readonly_fields", cls.readonly_fields)
47diff --git a/tests/regressiontests/admin_validation/tests.py b/tests/regressiontests/admin_validation/tests.py
48index 2cf3c60..9e50a8b 100644
49--- a/tests/regressiontests/admin_validation/tests.py
50+++ b/tests/regressiontests/admin_validation/tests.py
51@@ -92,10 +92,22 @@ class ValidationTestCase(TestCase):
52             inlines = [SongInline]
53 
54         self.assertRaisesMessage(ImproperlyConfigured,
55-            "SongInline cannot exclude the field 'album' - this is the foreign key to the parent model Album.",
56+            "SongInline cannot exclude the field 'album' - this is the foreign key to the parent model admin_validation.Album.",
57             validate,
58             AlbumAdmin, Album)
59 
60+    def test_app_label_in_admin_validation(self):
61+        """
62+        Regression test for #15669 - Include app label in admin validation messages
63+        """
64+        class RawIdNonexistingAdmin(admin.ModelAdmin):
65+            raw_id_fields = ('nonexisting',)
66+
67+        self.assertRaisesMessage(ImproperlyConfigured,
68+            "'RawIdNonexistingAdmin.raw_id_fields' refers to field 'nonexisting' that is missing from model 'admin_validation.Album'.",
69+            validate,
70+            RawIdNonexistingAdmin, Album)
71+
72     def test_fk_exclusion(self):
73         """
74         Regression test for #11709 - when testing for fk excluding (when exclude is
75diff --git a/tests/regressiontests/modeladmin/tests.py b/tests/regressiontests/modeladmin/tests.py
76index 7387a95..9de9693 100644
77--- a/tests/regressiontests/modeladmin/tests.py
78+++ b/tests/regressiontests/modeladmin/tests.py
79@@ -346,7 +346,7 @@ class ValidationTests(unittest.TestCase):
80 
81         self.assertRaisesRegexp(
82             ImproperlyConfigured,
83-            "'ValidationTestModelAdmin.raw_id_fields' refers to field 'non_existent_field' that is missing from model 'ValidationTestModel'.",
84+            "'ValidationTestModelAdmin.raw_id_fields' refers to field 'non_existent_field' that is missing from model 'modeladmin.ValidationTestModel'.",
85             validate,
86             ValidationTestModelAdmin,
87             ValidationTestModel,
88@@ -574,7 +574,7 @@ class ValidationTests(unittest.TestCase):
89 
90         self.assertRaisesRegexp(
91             ImproperlyConfigured,
92-            "'ValidationTestModelAdmin.filter_vertical' refers to field 'non_existent_field' that is missing from model 'ValidationTestModel'.",
93+            "'ValidationTestModelAdmin.filter_vertical' refers to field 'non_existent_field' that is missing from model 'modeladmin.ValidationTestModel'.",
94             validate,
95             ValidationTestModelAdmin,
96             ValidationTestModel,
97@@ -614,7 +614,7 @@ class ValidationTests(unittest.TestCase):
98 
99         self.assertRaisesRegexp(
100             ImproperlyConfigured,
101-            "'ValidationTestModelAdmin.filter_horizontal' refers to field 'non_existent_field' that is missing from model 'ValidationTestModel'.",
102+            "'ValidationTestModelAdmin.filter_horizontal' refers to field 'non_existent_field' that is missing from model 'modeladmin.ValidationTestModel'.",
103             validate,
104             ValidationTestModelAdmin,
105             ValidationTestModel,
106@@ -654,7 +654,7 @@ class ValidationTests(unittest.TestCase):
107 
108         self.assertRaisesRegexp(
109             ImproperlyConfigured,
110-            "'ValidationTestModelAdmin.radio_fields' refers to field 'non_existent_field' that is missing from model 'ValidationTestModel'.",
111+            "'ValidationTestModelAdmin.radio_fields' refers to field 'non_existent_field' that is missing from model 'modeladmin.ValidationTestModel'.",
112             validate,
113             ValidationTestModelAdmin,
114             ValidationTestModel,
115@@ -705,7 +705,7 @@ class ValidationTests(unittest.TestCase):
116 
117         self.assertRaisesRegexp(
118             ImproperlyConfigured,
119-            "'ValidationTestModelAdmin.prepopulated_fields' refers to field 'non_existent_field' that is missing from model 'ValidationTestModel'.",
120+            "'ValidationTestModelAdmin.prepopulated_fields' refers to field 'non_existent_field' that is missing from model 'modeladmin.ValidationTestModel'.",
121             validate,
122             ValidationTestModelAdmin,
123             ValidationTestModel,
124@@ -716,7 +716,7 @@ class ValidationTests(unittest.TestCase):
125 
126         self.assertRaisesRegexp(
127             ImproperlyConfigured,
128-            "'ValidationTestModelAdmin.prepopulated_fields\['slug'\]\[0\]' refers to field 'non_existent_field' that is missing from model 'ValidationTestModel'.",
129+            "'ValidationTestModelAdmin.prepopulated_fields\['slug'\]\[0\]' refers to field 'non_existent_field' that is missing from model 'modeladmin.ValidationTestModel'.",
130             validate,
131             ValidationTestModelAdmin,
132             ValidationTestModel,
133@@ -953,7 +953,7 @@ class ValidationTests(unittest.TestCase):
134 
135         self.assertRaisesRegexp(
136             ImproperlyConfigured,
137-            "'ValidationTestModelAdmin.date_hierarchy' refers to field 'non_existent_field' that is missing from model 'ValidationTestModel'.",
138+            "'ValidationTestModelAdmin.date_hierarchy' refers to field 'non_existent_field' that is missing from model 'modeladmin.ValidationTestModel'.",
139             validate,
140             ValidationTestModelAdmin,
141             ValidationTestModel,
142@@ -993,7 +993,7 @@ class ValidationTests(unittest.TestCase):
143 
144         self.assertRaisesRegexp(
145             ImproperlyConfigured,
146-            "'ValidationTestModelAdmin.ordering\[0\]' refers to field 'non_existent_field' that is missing from model 'ValidationTestModel'.",
147+            "'ValidationTestModelAdmin.ordering\[0\]' refers to field 'non_existent_field' that is missing from model 'modeladmin.ValidationTestModel'.",
148             validate,
149             ValidationTestModelAdmin,
150             ValidationTestModel,
151@@ -1188,7 +1188,7 @@ class ValidationTests(unittest.TestCase):
152 
153         self.assertRaisesRegexp(
154             ImproperlyConfigured,
155-            "'ValidationTestInline.fk_name' refers to field 'non_existent_field' that is missing from model 'ValidationTestInlineModel'.",
156+            "'ValidationTestInline.fk_name' refers to field 'non_existent_field' that is missing from model 'modeladmin.ValidationTestInlineModel'.",
157             validate,
158             ValidationTestModelAdmin,
159             ValidationTestModel,