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

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

    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):  
    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..f370a38 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)
    4555    name = models.CharField(max_length=200)
    class Worker(models.Model):  
    4757    def __unicode__(self):
    4858        return self.name
    4959
     60
    5061class BrokenUnicodeMethod(models.Model):
    5162    name = models.CharField(max_length=7)
    5263
    class BrokenUnicodeMethod(models.Model):  
    5566        # object).
    5667        return 'Názov: %s' % self.name
    5768
     69
    5870class NonAutoPK(models.Model):
    5971    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..5776691 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        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
    167177
    168178class ModelValidationTest(TestCase):
    169179    def test_pk_validation(self):
Back to Top