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):
|
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/forms/forms.py b/tests/regressiontests/forms/forms.py
new file mode 100644
index 0000000..ff72d88
-
|
+
|
|
| 1 | from django.forms import FileField, Form, ModelForm |
| 2 | |
| 3 | from .models import Cheese, ChoiceFieldModel |
| 4 | |
| 5 | |
| 6 | class CheeseForm(ModelForm): |
| 7 | class Meta: |
| 8 | model = Cheese |
| 9 | |
| 10 | |
| 11 | class ChoiceFieldForm(ModelForm): |
| 12 | class Meta: |
| 13 | model = ChoiceFieldModel |
| 14 | |
| 15 | |
| 16 | class FileForm(Form): |
| 17 | file1 = FileField() |
diff --git a/tests/regressiontests/forms/models.py b/tests/regressiontests/forms/models.py
index 9390557..200ade3 100644
a
|
b
|
class ChoiceOptionModel(models.Model):
|
41 | 41 | |
42 | 42 | class Meta: |
43 | 43 | ordering = ('name',) |
| 44 | verbose_name = ('Choice Option') # regression t5373 |
44 | 45 | |
45 | 46 | def __unicode__(self): |
46 | 47 | return u'ChoiceOption %d' % self.pk |
diff --git a/tests/regressiontests/forms/tests/models.py b/tests/regressiontests/forms/tests/models.py
index b5e2c3a..1dca1bc 100644
a
|
b
|
import datetime
|
5 | 5 | |
6 | 6 | from django.core.files.uploadedfile import SimpleUploadedFile |
7 | 7 | from django.db import models |
8 | | from django.forms import Form, ModelForm, FileField, ModelChoiceField |
| 8 | from django.forms import ModelForm, ModelChoiceField |
9 | 9 | from django.forms.models import ModelFormMetaclass |
10 | 10 | from django.test import TestCase |
11 | 11 | |
12 | | from ..models import (ChoiceOptionModel, ChoiceFieldModel, FileModel, Group, |
| 12 | from ..forms import ChoiceFieldForm, FileForm |
| 13 | from ..models import (ChoiceOptionModel, FileModel, Group, |
13 | 14 | BoundaryModel, Defaults) |
14 | 15 | |
15 | 16 | |
16 | | class ChoiceFieldForm(ModelForm): |
17 | | class Meta: |
18 | | model = ChoiceFieldModel |
19 | | |
20 | | |
21 | | class FileForm(Form): |
22 | | file1 = FileField() |
23 | | |
24 | | |
25 | 17 | class TestTicket12510(TestCase): |
26 | 18 | ''' It is not necessary to generate choices for ModelChoiceField (regression test for #12510). ''' |
27 | 19 | def setUp(self): |
… |
… |
class TestTicket12510(TestCase):
|
33 | 25 | field = ModelChoiceField(Group.objects.order_by('-name')) |
34 | 26 | self.assertEqual('a', field.clean(self.groups[0].pk).name) |
35 | 27 | |
| 28 | |
36 | 29 | class ModelFormCallableModelDefault(TestCase): |
37 | 30 | def test_no_empty_option(self): |
38 | 31 | "If a model's ForeignKey has blank=False and a default, no empty option is created (Refs #10792)." |
… |
… |
class ModelFormCallableModelDefault(TestCase):
|
102 | 95 | <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>""") |
103 | 96 | |
104 | 97 | |
105 | | |
106 | 98 | class FormsModelTestCase(TestCase): |
107 | 99 | def test_unicode_filename(self): |
108 | 100 | # FileModel with unicode filename and data ######################### |
… |
… |
class FormsModelTestCase(TestCase):
|
166 | 158 | self.assertEqual(obj.value, 99) |
167 | 159 | self.assertEqual(obj.def_date, datetime.date(1999, 3, 2)) |
168 | 160 | |
| 161 | |
169 | 162 | class RelatedModelFormTests(TestCase): |
170 | 163 | def test_invalid_loading_order(self): |
171 | 164 | """ |
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 *
|
4 | 4 | from django.test import TestCase |
5 | 5 | from django.utils.translation import ugettext_lazy, override |
6 | 6 | |
7 | | from regressiontests.forms.models import Cheese |
| 7 | from ..forms import CheeseForm, ChoiceFieldForm |
| 8 | from ..models import ChoiceOptionModel |
8 | 9 | |
9 | 10 | |
10 | 11 | class FormsRegressionsTestCase(TestCase): |
… |
… |
class FormsRegressionsTestCase(TestCase):
|
129 | 130 | a pk uniqueness error. |
130 | 131 | |
131 | 132 | """ |
132 | | class CheeseForm(ModelForm): |
133 | | class Meta: |
134 | | model = Cheese |
135 | | |
136 | | form = CheeseForm({ |
137 | | 'name': 'Brie', |
138 | | }) |
139 | | |
| 133 | form = CheeseForm({'name': 'Brie'}) |
140 | 134 | self.assertTrue(form.is_valid()) |
141 | 135 | |
142 | 136 | obj = form.save() |
143 | 137 | obj.name = 'Camembert' |
144 | 138 | 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) |