Made m2m fields form help_text munging specific to admin widgets.

Refs #9321 and follow-up to e80de93af6.
This commit is contained in:
Ramiro Morales 2013-05-19 16:28:33 -03:00
parent e61d99d96d
commit 491419b5ff
2 changed files with 20 additions and 1 deletions

View File

@ -30,6 +30,7 @@ from django.db.models.sql.constants import QUERY_TERMS
from django.forms.formsets import all_valid, DELETION_FIELD_NAME from django.forms.formsets import all_valid, DELETION_FIELD_NAME
from django.forms.models import (modelform_factory, modelformset_factory, from django.forms.models import (modelform_factory, modelformset_factory,
inlineformset_factory, BaseInlineFormSet, modelform_defines_fields) inlineformset_factory, BaseInlineFormSet, modelform_defines_fields)
from django.forms.widgets import SelectMultiple, CheckboxSelectMultiple
from django.http import Http404, HttpResponseRedirect from django.http import Http404, HttpResponseRedirect
from django.http.response import HttpResponseBase from django.http.response import HttpResponseBase
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
@ -41,6 +42,7 @@ from django.utils.encoding import force_text, python_2_unicode_compatible
from django.utils.html import escape, escapejs from django.utils.html import escape, escapejs
from django.utils.http import urlencode from django.utils.http import urlencode
from django.utils.text import capfirst, get_text_list from django.utils.text import capfirst, get_text_list
from django.utils.translation import string_concat
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.utils.translation import ungettext from django.utils.translation import ungettext
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
@ -280,7 +282,12 @@ class BaseModelAdmin(six.with_metaclass(forms.MediaDefiningClass)):
if queryset is not None: if queryset is not None:
kwargs['queryset'] = queryset kwargs['queryset'] = queryset
return db_field.formfield(**kwargs) form_field = db_field.formfield(**kwargs)
if isinstance(form_field.widget, SelectMultiple) and not isinstance(form_field.widget, CheckboxSelectMultiple):
msg = _('Hold down "Control", or "Command" on a Mac, to select more than one.')
help_text = form_field.help_text
form_field.help_text = string_concat(help_text, ' ', msg) if help_text else msg
return form_field
def get_view_on_site_url(self, obj=None): def get_view_on_site_url(self, obj=None):
if obj is None or not self.view_on_site: if obj is None or not self.view_on_site:

View File

@ -19,6 +19,7 @@ from django.core.files.uploadedfile import SimpleUploadedFile
from django.db.models import CharField, DateField from django.db.models import CharField, DateField
from django.test import TestCase as DjangoTestCase from django.test import TestCase as DjangoTestCase
from django.test import override_settings from django.test import override_settings
from django.utils import six
from django.utils import translation from django.utils import translation
from . import models from . import models
@ -168,6 +169,17 @@ class AdminFormfieldForDBFieldTests(TestCase):
def testInheritance(self): def testInheritance(self):
self.assertFormfield(models.Album, 'backside_art', widgets.AdminFileWidget) self.assertFormfield(models.Album, 'backside_art', widgets.AdminFileWidget)
def test_m2m_widgets(self):
"""m2m fields help text as it applies to admin app (#9321)."""
class AdvisorAdmin(admin.ModelAdmin):
filter_vertical = ['companies']
self.assertFormfield(models.Advisor, 'companies', widgets.FilteredSelectMultiple,
filter_vertical=['companies'])
ma = AdvisorAdmin(models.Advisor, admin.site)
f = ma.formfield_for_dbfield(models.Advisor._meta.get_field('companies'), request=None)
self.assertEqual(six.text_type(f.help_text), 'Hold down "Control", or "Command" on a Mac, to select more than one.')
@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',)) @override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
class AdminFormfieldForDBFieldWithRequestTests(DjangoTestCase): class AdminFormfieldForDBFieldWithRequestTests(DjangoTestCase):