Ticket #5373: 0001-Regression-for-5373-with-tests.patch

File 0001-Regression-for-5373-with-tests.patch, 6.8 KB (added by Adrien Lemaire, 13 years ago)

patch udpated + test

  • django/db/models/fields/__init__.py

    From 7a1a1d89541fb2a79321317bf51b21ea2f025287 Mon Sep 17 00:00:00 2001
    From: Adrien Lemaire <lemaire.adrien@gmail.com>
    Date: Sun, 25 Mar 2012 16:41:43 +0900
    Subject: [PATCH] Regression for #5373 with tests
    
    I also cleaned a bit regressiontests/forms, added a forms.py and moved some
    forms from models.py and tests/models.py inside.
    ---
     django/db/models/fields/__init__.py              |    7 +++++--
     django/db/models/fields/related.py               |    7 +++++++
     tests/regressiontests/forms/forms.py             |   17 +++++++++++++++++
     tests/regressiontests/forms/models.py            |    1 +
     tests/regressiontests/forms/tests/models.py      |   17 +++++------------
     tests/regressiontests/forms/tests/regressions.py |   19 ++++++++++---------
     6 files changed, 45 insertions(+), 23 deletions(-)
     create mode 100644 tests/regressiontests/forms/forms.py
    
    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:
  • new file tests/regressiontests/forms/forms.py

    diff --git a/tests/regressiontests/forms/forms.py b/tests/regressiontests/forms/forms.py
    new file mode 100644
    index 0000000..ff72d88
    - +  
     1from django.forms import FileField, Form, ModelForm
     2
     3from .models import Cheese, ChoiceFieldModel
     4
     5
     6class CheeseForm(ModelForm):
     7    class Meta:
     8        model = Cheese
     9
     10
     11class ChoiceFieldForm(ModelForm):
     12    class Meta:
     13        model = ChoiceFieldModel
     14
     15
     16class FileForm(Form):
     17    file1 = FileField()
  • tests/regressiontests/forms/models.py

    diff --git a/tests/regressiontests/forms/models.py b/tests/regressiontests/forms/models.py
    index 9390557..200ade3 100644
    a b class ChoiceOptionModel(models.Model):  
    4141
    4242    class Meta:
    4343        ordering = ('name',)
     44        verbose_name = ('Choice Option')  #  regression t5373
    4445
    4546    def __unicode__(self):
    4647        return u'ChoiceOption %d' % self.pk
  • tests/regressiontests/forms/tests/models.py

    diff --git a/tests/regressiontests/forms/tests/models.py b/tests/regressiontests/forms/tests/models.py
    index b5e2c3a..1dca1bc 100644
    a b import datetime  
    55
    66from django.core.files.uploadedfile import SimpleUploadedFile
    77from django.db import models
    8 from django.forms import Form, ModelForm, FileField, ModelChoiceField
     8from django.forms import ModelForm, ModelChoiceField
    99from django.forms.models import ModelFormMetaclass
    1010from django.test import TestCase
    1111
    12 from ..models import (ChoiceOptionModel, ChoiceFieldModel, FileModel, Group,
     12from ..forms import ChoiceFieldForm, FileForm
     13from ..models import (ChoiceOptionModel, FileModel, Group,
    1314    BoundaryModel, Defaults)
    1415
    1516
    16 class ChoiceFieldForm(ModelForm):
    17     class Meta:
    18         model = ChoiceFieldModel
    19 
    20 
    21 class FileForm(Form):
    22     file1 = FileField()
    23 
    24 
    2517class TestTicket12510(TestCase):
    2618    ''' It is not necessary to generate choices for ModelChoiceField (regression test for #12510). '''
    2719    def setUp(self):
    class TestTicket12510(TestCase):  
    3325            field = ModelChoiceField(Group.objects.order_by('-name'))
    3426            self.assertEqual('a', field.clean(self.groups[0].pk).name)
    3527
     28
    3629class ModelFormCallableModelDefault(TestCase):
    3730    def test_no_empty_option(self):
    3831        "If a model's ForeignKey has blank=False and a default, no empty option is created (Refs #10792)."
    class ModelFormCallableModelDefault(TestCase):  
    10295<input type="hidden" name="initial-multi_choice_int" value="3" id="initial-id_multi_choice_int_1" /> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>""")
    10396
    10497
    105 
    10698class FormsModelTestCase(TestCase):
    10799    def test_unicode_filename(self):
    108100        # FileModel with unicode filename and data #########################
    class FormsModelTestCase(TestCase):  
    166158        self.assertEqual(obj.value, 99)
    167159        self.assertEqual(obj.def_date, datetime.date(1999, 3, 2))
    168160
     161
    169162class RelatedModelFormTests(TestCase):
    170163    def test_invalid_loading_order(self):
    171164        """
  • tests/regressiontests/forms/tests/regressions.py

    diff --git a/tests/regressiontests/forms/tests/regressions.py b/tests/regressiontests/forms/tests/regressions.py
    index a93781d..3ffc120 100644
    a b from django.forms import *  
    44from django.test import TestCase
    55from django.utils.translation import ugettext_lazy, override
    66
    7 from regressiontests.forms.models import Cheese
     7from ..forms import CheeseForm, ChoiceFieldForm
     8from ..models import ChoiceOptionModel
    89
    910
    1011class FormsRegressionsTestCase(TestCase):
    class FormsRegressionsTestCase(TestCase):  
    129130        a pk uniqueness error.
    130131
    131132        """
    132         class CheeseForm(ModelForm):
    133             class Meta:
    134                 model = Cheese
    135 
    136         form = CheeseForm({
    137             'name': 'Brie',
    138         })
    139 
     133        form = CheeseForm({'name': 'Brie'})
    140134        self.assertTrue(form.is_valid())
    141135
    142136        obj = form.save()
    143137        obj.name = 'Camembert'
    144138        obj.full_clean()
     139
     140    def test_regression_5373(self):
     141        """Field label for a ForeignKey should be translated"""
     142        ChoiceOptionModel.objects.create(id=1, name='default')
     143        form = ChoiceFieldForm()
     144        self.assertEqual(form.fields['choice'].label,
     145            ChoiceOptionModel._meta.verbose_name)
Back to Top