From a5aa707a2ac522611d6f7191833fa9149ab25de0 Mon Sep 17 00:00:00 2001
From: Renato Alves <rjalves@igc.gulbenkian.pt>
Date: Wed, 3 Oct 2012 13:44:59 +0100
Subject: [PATCH] Allow overriding help_text default message on ManyToMany
fields
---
django/db/models/fields/related.py | 5 ++-
tests/modeltests/model_forms/models.py | 12 ++++++++
tests/modeltests/model_forms/tests.py | 47 ++++++++++++++++++++++++++++++-
3 files changed, 60 insertions(+), 4 deletions(-)
diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
index 157640c..b4060df 100644
a
|
b
|
class ManyToManyField(RelatedField, Field):
|
1162 | 1162 | |
1163 | 1163 | Field.__init__(self, **kwargs) |
1164 | 1164 | |
1165 | | msg = _('Hold down "Control", or "Command" on a Mac, to select more than one.') |
1166 | | self.help_text = string_concat(self.help_text, ' ', msg) |
| 1165 | # Only display "Hold down..." message if an empty string is passed. |
| 1166 | if not str(self.help_text): |
| 1167 | self.help_text = _('Hold down "Control", or "Command" on a Mac, to select more than one.') |
1167 | 1168 | |
1168 | 1169 | def get_choices_default(self): |
1169 | 1170 | return Field.get_choices(self, include_blank=False) |
diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py
index 90b019b..134cab7 100644
a
|
b
|
class FlexibleDatePost(models.Model):
|
272 | 272 | slug = models.CharField(max_length=50, unique_for_year='posted', blank=True) |
273 | 273 | subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True) |
274 | 274 | posted = models.DateField(blank=True, null=True) |
| 275 | |
| 276 | class M2MDefaultHelpText(models.Model): |
| 277 | ref = models.ManyToManyField("self", blank=True) |
| 278 | |
| 279 | class M2MNoHelpText(models.Model): |
| 280 | ref = models.ManyToManyField("self", blank=True, help_text=None) |
| 281 | |
| 282 | class M2MCustomHelpText(models.Model): |
| 283 | ref = models.ManyToManyField("self", blank=True, help_text="Help") |
| 284 | |
| 285 | class M2MEmptyHelpText(models.Model): |
| 286 | ref = models.ManyToManyField("self", blank=True, help_text="") |
diff --git a/tests/modeltests/model_forms/tests.py b/tests/modeltests/model_forms/tests.py
index 038ce32..8ce299b 100644
a
|
b
|
from .models import (Article, ArticleStatus, BetterWriter, BigInt, Book,
|
17 | 17 | Category, CommaSeparatedInteger, CustomFieldForExclusionModel, DerivedBook, |
18 | 18 | DerivedPost, ExplicitPK, FlexibleDatePost, ImprovedArticle, |
19 | 19 | ImprovedArticleWithParentLink, Inventory, PhoneNumber, Post, Price, |
20 | | Product, TextFile, Writer, WriterProfile, test_images) |
| 20 | Product, TextFile, Writer, WriterProfile, test_images, M2MDefaultHelpText, |
| 21 | M2MNoHelpText, M2MCustomHelpText, M2MEmptyHelpText) |
21 | 22 | |
22 | 23 | if test_images: |
23 | 24 | from .models import ImageFile, OptionalImageFile |
… |
… |
class PriceFormWithoutQuantity(forms.ModelForm):
|
176 | 177 | model = Price |
177 | 178 | exclude = ('quantity',) |
178 | 179 | |
179 | | |
180 | 180 | class ModelFormBaseTest(TestCase): |
181 | 181 | def test_base_form(self): |
182 | 182 | self.assertEqual(list(BaseCategoryForm.base_fields), |
… |
… |
class ModelFormBaseTest(TestCase):
|
330 | 330 | self.assertEqual(list(OrderFields2.base_fields), |
331 | 331 | ['slug', 'name']) |
332 | 332 | |
| 333 | def test_default_M2M_helptext(self): |
| 334 | class DefaultHelpText(forms.ModelForm): |
| 335 | class Meta: |
| 336 | model = M2MDefaultHelpText |
| 337 | |
| 338 | self.assertHTMLEqual( |
| 339 | str(DefaultHelpText()), |
| 340 | """<tr><th><label for="id_ref">Ref:</label></th><td><select id="id_ref" multiple="multiple" name="ref" /><br /><span class="helptext"> |
| 341 | Hold down "Control", or "Command" on a Mac, to select more than one.</span></td></tr>""" |
| 342 | ) |
| 343 | |
| 344 | def test_override_M2M_helptext(self): |
| 345 | class NoHelpText(forms.ModelForm): |
| 346 | class Meta: |
| 347 | model = M2MNoHelpText |
| 348 | |
| 349 | self.assertHTMLEqual( |
| 350 | str(NoHelpText()), |
| 351 | """<tr><th><label for="id_ref">Ref:</label></th><td><select id="id_ref" multiple="multiple" name="ref" /></td></tr>""" |
| 352 | ) |
| 353 | |
| 354 | |
| 355 | def test_custom_M2M_helptext(self): |
| 356 | class CustomHelpText(forms.ModelForm): |
| 357 | class Meta: |
| 358 | model = M2MCustomHelpText |
| 359 | |
| 360 | self.assertHTMLEqual( |
| 361 | str(CustomHelpText()), |
| 362 | """<tr><th><label for="id_ref">Ref:</label></th><td><select id="id_ref" multiple="multiple" name="ref" /><br /><span class="helptext">Help</span></td></tr>""" |
| 363 | ) |
| 364 | |
| 365 | def test_empty_M2M_helptext(self): |
| 366 | class EmptyHelpText(forms.ModelForm): |
| 367 | class Meta: |
| 368 | model = M2MEmptyHelpText |
| 369 | |
| 370 | self.assertHTMLEqual( |
| 371 | str(EmptyHelpText()), |
| 372 | """<tr><th><label for="id_ref">Ref:</label></th><td><select id="id_ref" multiple="multiple" name="ref" /><br /><span class="helptext"> |
| 373 | Hold down "Control", or "Command" on a Mac, to select more than one.</span></td></tr>""" |
| 374 | ) |
| 375 | |
333 | 376 | |
334 | 377 | class TestWidgetForm(forms.ModelForm): |
335 | 378 | class Meta: |