Code

Ticket #5373: 5373.6.diff

File 5373.6.diff, 1.5 KB (added by Roald de Vries <roald@…>, 3 years ago)

Improvement of prior solutions, but no final fix

Line 
1diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
2index 9037265..8619008 100644
3--- a/django/db/models/fields/__init__.py
4+++ b/django/db/models/fields/__init__.py
5@@ -222,11 +222,14 @@ class Field(object):
6         return self._unique or self.primary_key
7     unique = property(unique)
8 
9+    def set_verbose_name_from_name(self, name):
10+        if self.verbose_name is None and name:
11+            self.verbose_name = name.replace('_', ' ')
12+
13     def set_attributes_from_name(self, name):
14         self.name = name
15         self.attname, self.column = self.get_attname_column()
16-        if self.verbose_name is None and name:
17-            self.verbose_name = name.replace('_', ' ')
18+        self.set_verbose_name_from_name(name)
19 
20     def contribute_to_class(self, cls, name):
21         self.set_attributes_from_name(name)
22diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
23index cedf308..774a724 100644
24--- a/django/db/models/fields/related.py
25+++ b/django/db/models/fields/related.py
26@@ -112,6 +112,10 @@ class RelatedField(object):
27         else:
28             self.do_related_class(other, cls)
29 
30+    def set_verbose_name_from_name(self, name):
31+        # for related fields, the verbose name should be set by set_attributes_from_rel
32+        pass
33+
34     def set_attributes_from_rel(self):
35         self.name = self.name or (self.rel.to._meta.object_name.lower() + '_' + self.rel.to._meta.pk.name)
36         if self.verbose_name is None: