From e0e9a7346dcedd314934af40684ec6f03f1e49cb Mon Sep 17 00:00:00 2001
From: Adrien Lemaire <lemaire.adrien@gmail.com>
Date: Mon, 26 Mar 2012 09:30:31 +0900
Subject: [PATCH] Fix #5373 + test at model level
---
django/db/models/fields/__init__.py | 7 +++++--
django/db/models/fields/related.py | 7 +++++++
tests/regressiontests/model_regress/models.py | 12 ++++++++++++
tests/regressiontests/model_regress/tests.py | 10 ++++++++++
4 files changed, 34 insertions(+), 2 deletions(-)
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index 22546c2..c7d8c12 100644
a
|
b
|
class Field(object):
|
232 | 232 | def unique(self): |
233 | 233 | return self._unique or self.primary_key |
234 | 234 | |
| 235 | def set_verbose_name_from_name(self, name): |
| 236 | if self.verbose_name is None and name: |
| 237 | self.verbose_name = name.replace('_', ' ') |
| 238 | |
235 | 239 | def set_attributes_from_name(self, name): |
236 | 240 | if not self.name: |
237 | 241 | self.name = name |
238 | 242 | self.attname, self.column = self.get_attname_column() |
239 | | if self.verbose_name is None and self.name: |
240 | | self.verbose_name = self.name.replace('_', ' ') |
| 243 | self.set_verbose_name_from_name(name) |
241 | 244 | |
242 | 245 | def contribute_to_class(self, cls, name): |
243 | 246 | self.set_attributes_from_name(name) |
diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
index e23c7dc..57d5d91 100644
a
|
b
|
class RelatedField(object):
|
112 | 112 | else: |
113 | 113 | self.do_related_class(other, cls) |
114 | 114 | |
| 115 | def set_verbose_name_from_name(self, name): |
| 116 | """ |
| 117 | for related fields, the verbose name should be set by |
| 118 | set_attributes_from_rel |
| 119 | """ |
| 120 | pass |
| 121 | |
115 | 122 | def set_attributes_from_rel(self): |
116 | 123 | self.name = self.name or (self.rel.to._meta.object_name.lower() + '_' + self.rel.to._meta.pk.name) |
117 | 124 | if self.verbose_name is None: |
diff --git a/tests/regressiontests/model_regress/models.py b/tests/regressiontests/model_regress/models.py
index f30b3ee..f370a38 100644
a
|
b
|
CHOICES = (
|
7 | 7 | (2, 'second'), |
8 | 8 | ) |
9 | 9 | |
| 10 | |
10 | 11 | class Article(models.Model): |
11 | 12 | headline = models.CharField(max_length=100, default='Default headline') |
12 | 13 | pub_date = models.DateTimeField() |
… |
… |
class Article(models.Model):
|
22 | 23 | def __unicode__(self): |
23 | 24 | return self.headline |
24 | 25 | |
| 26 | |
25 | 27 | class Movie(models.Model): |
26 | 28 | #5218: Test models with non-default primary keys / AutoFields |
27 | 29 | movie_id = models.AutoField(primary_key=True) |
28 | 30 | name = models.CharField(max_length=60) |
29 | 31 | |
| 32 | |
30 | 33 | class Party(models.Model): |
31 | 34 | when = models.DateField(null=True) |
32 | 35 | |
| 36 | |
33 | 37 | class Event(models.Model): |
34 | 38 | when = models.DateTimeField() |
35 | 39 | |
| 40 | |
36 | 41 | class Department(models.Model): |
37 | 42 | id = models.PositiveIntegerField(primary_key=True) |
38 | 43 | name = models.CharField(max_length=200) |
39 | 44 | |
| 45 | class Meta: |
| 46 | """Used for regression t5373""" |
| 47 | verbose_name = ('Fancy Department Name') |
| 48 | |
40 | 49 | def __unicode__(self): |
41 | 50 | return self.name |
42 | 51 | |
| 52 | |
43 | 53 | class Worker(models.Model): |
44 | 54 | department = models.ForeignKey(Department) |
45 | 55 | name = models.CharField(max_length=200) |
… |
… |
class Worker(models.Model):
|
47 | 57 | def __unicode__(self): |
48 | 58 | return self.name |
49 | 59 | |
| 60 | |
50 | 61 | class BrokenUnicodeMethod(models.Model): |
51 | 62 | name = models.CharField(max_length=7) |
52 | 63 | |
… |
… |
class BrokenUnicodeMethod(models.Model):
|
55 | 66 | # object). |
56 | 67 | return 'Názov: %s' % self.name |
57 | 68 | |
| 69 | |
58 | 70 | class NonAutoPK(models.Model): |
59 | 71 | name = models.CharField(max_length=10, primary_key=True) |
diff --git a/tests/regressiontests/model_regress/tests.py b/tests/regressiontests/model_regress/tests.py
index 77bb4f2..5776691 100644
a
|
b
|
class ModelTests(TestCase):
|
164 | 164 | 1 |
165 | 165 | ) |
166 | 166 | |
| 167 | def test_regression_5373(self): |
| 168 | """Field label for a ForeignKey should be translated""" |
| 169 | department = Department.objects.create(id=1) |
| 170 | worker = Worker.objects.create(department=department) |
| 171 | self.assertEqual( |
| 172 | worker._meta.get_field('department').verbose_name, |
| 173 | 'Fancy Department Name', |
| 174 | msg='patch #5373 fix bad verbose_name for ForeignKeys', |
| 175 | ) |
| 176 | |
167 | 177 | |
168 | 178 | class ModelValidationTest(TestCase): |
169 | 179 | def test_pk_validation(self): |