From f36651986b5fcc97854402f0a0d3725fbf5151fa Mon Sep 17 00:00:00 2001
From: Adrien Lemaire <lemaire.adrien@gmail.com>
Date: Tue, 27 Mar 2012 11:37:07 +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 | 17 +++++++++++++++++
tests/regressiontests/model_regress/tests.py | 19 +++++++++++++++++++
4 files changed, 48 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..aa8ea24 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) |
| 55 | fake_department = models.ForeignKey( # for t5373 |
| 56 | Department, related_name='fake_worker', |
| 57 | verbose_name=('Fake Department'), |
| 58 | blank=True, null=True, |
| 59 | ) |
45 | 60 | name = models.CharField(max_length=200) |
46 | 61 | |
47 | 62 | def __unicode__(self): |
48 | 63 | return self.name |
49 | 64 | |
| 65 | |
50 | 66 | class BrokenUnicodeMethod(models.Model): |
51 | 67 | name = models.CharField(max_length=7) |
52 | 68 | |
… |
… |
class BrokenUnicodeMethod(models.Model):
|
55 | 71 | # object). |
56 | 72 | return 'Názov: %s' % self.name |
57 | 73 | |
| 74 | |
58 | 75 | class NonAutoPK(models.Model): |
59 | 76 | 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..8d88444 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 | fake_department = Department.objects.create(id=2) |
| 171 | worker = Worker.objects.create( |
| 172 | department=department, |
| 173 | fake_department=fake_department, |
| 174 | ) |
| 175 | self.assertEqual( |
| 176 | worker._meta.get_field('department').verbose_name, |
| 177 | 'Fancy Department Name', |
| 178 | msg='patch #5373 fix bad verbose_name for ForeignKeys', |
| 179 | ) |
| 180 | self.assertEqual( |
| 181 | worker._meta.get_field('fake_department').verbose_name, |
| 182 | 'Fake Department', |
| 183 | msg='ForeignKey verbose_name overrides Model verbose_name', |
| 184 | ) |
| 185 | |
167 | 186 | |
168 | 187 | class ModelValidationTest(TestCase): |
169 | 188 | def test_pk_validation(self): |