Ticket #5373: 0003-Fix-5373-test-at-model-level.patch

File 0003-Fix-5373-test-at-model-level.patch, 5.0 KB (added by Adrien Lemaire, 13 years ago)
  • django/db/models/fields/__init__.py

    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):  
    232232    def unique(self):
    233233        return self._unique or self.primary_key
    234234
     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
    235239    def set_attributes_from_name(self, name):
    236240        if not self.name:
    237241            self.name = name
    238242        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)
    241244
    242245    def contribute_to_class(self, cls, name):
    243246        self.set_attributes_from_name(name)
  • django/db/models/fields/related.py

    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):  
    112112        else:
    113113            self.do_related_class(other, cls)
    114114
     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
    115122    def set_attributes_from_rel(self):
    116123        self.name = self.name or (self.rel.to._meta.object_name.lower() + '_' + self.rel.to._meta.pk.name)
    117124        if self.verbose_name is None:
  • tests/regressiontests/model_regress/models.py

    diff --git a/tests/regressiontests/model_regress/models.py b/tests/regressiontests/model_regress/models.py
    index f30b3ee..aa8ea24 100644
    a b CHOICES = (  
    77    (2, 'second'),
    88)
    99
     10
    1011class Article(models.Model):
    1112    headline = models.CharField(max_length=100, default='Default headline')
    1213    pub_date = models.DateTimeField()
    class Article(models.Model):  
    2223    def __unicode__(self):
    2324        return self.headline
    2425
     26
    2527class Movie(models.Model):
    2628    #5218: Test models with non-default primary keys / AutoFields
    2729    movie_id = models.AutoField(primary_key=True)
    2830    name = models.CharField(max_length=60)
    2931
     32
    3033class Party(models.Model):
    3134    when = models.DateField(null=True)
    3235
     36
    3337class Event(models.Model):
    3438    when = models.DateTimeField()
    3539
     40
    3641class Department(models.Model):
    3742    id = models.PositiveIntegerField(primary_key=True)
    3843    name = models.CharField(max_length=200)
    3944
     45    class Meta:
     46        """Used for regression t5373"""
     47        verbose_name = ('Fancy Department Name')
     48
    4049    def __unicode__(self):
    4150        return self.name
    4251
     52
    4353class Worker(models.Model):
    4454    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    )
    4560    name = models.CharField(max_length=200)
    4661
    4762    def __unicode__(self):
    4863        return self.name
    4964
     65
    5066class BrokenUnicodeMethod(models.Model):
    5167    name = models.CharField(max_length=7)
    5268
    class BrokenUnicodeMethod(models.Model):  
    5571        # object).
    5672        return 'Názov: %s' % self.name
    5773
     74
    5875class NonAutoPK(models.Model):
    5976    name = models.CharField(max_length=10, primary_key=True)
  • tests/regressiontests/model_regress/tests.py

    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):  
    164164            1
    165165        )
    166166
     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
    167186
    168187class ModelValidationTest(TestCase):
    169188    def test_pk_validation(self):
Back to Top