Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #8556: added a useful formfield to `CommaSeparatedIntegerField`…

…. gkelly, mattmcc, and kratorius all contributed portions of this patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8682 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a41ca9c9e284628c81f0a9b5a652a03ddbdd7521 1 parent fe8e00a
@jacobian jacobian authored
View
5 django/contrib/admin/options.py
@@ -94,6 +94,11 @@ def formfield_for_dbfield(self, db_field, **kwargs):
kwargs['widget'] = widgets.AdminIntegerFieldWidget
return db_field.formfield(**kwargs)
+ # For CommaSeparatedIntegerFields, add a custom CSS class.
+ if isinstance(db_field, models.CommaSeparatedIntegerField):
+ kwargs['widget'] = widgets.AdminCommaSeparatedIntegerFieldWidget
+ return db_field.formfield(**kwargs)
+
# For TextInputs, add a custom CSS class.
if isinstance(db_field, models.CharField):
kwargs['widget'] = widgets.AdminTextInputWidget
View
7 django/contrib/admin/widgets.py
@@ -236,3 +236,10 @@ def __init__(self, attrs=None):
if attrs is not None:
final_attrs.update(attrs)
super(AdminIntegerFieldWidget, self).__init__(attrs=final_attrs)
+
+class AdminCommaSeparatedIntegerFieldWidget(forms.TextInput):
+ def __init__(self, attrs=None):
+ final_attrs = {'class': 'vCommaSeparatedIntegerField'}
+ if attrs is not None:
+ final_attrs.update(attrs)
+ super(AdminCommaSeparatedIntegerFieldWidget, self).__init__(attrs=final_attrs)
View
12 django/db/models/fields/__init__.py
@@ -394,7 +394,17 @@ def formfield(self, **kwargs):
# TODO: Maybe move this into contrib, because it's specialized.
class CommaSeparatedIntegerField(CharField):
- pass
+ def formfield(self, **kwargs):
+ defaults = {
+ 'form_class': forms.RegexField,
+ 'regex': '^[\d,]+$',
+ 'max_length': self.max_length,
+ 'error_messages': {
+ 'invalid': _(u'Enter only digits separated by commas.'),
+ }
+ }
+ defaults.update(kwargs)
+ return super(CommaSeparatedIntegerField, self).formfield(**defaults)
ansi_date_re = re.compile(r'^\d{4}-\d{1,2}-\d{1,2}$')
View
32 tests/modeltests/model_forms/models.py
@@ -98,6 +98,12 @@ class ImageFile(models.Model):
def __unicode__(self):
return self.description
+class CommaSeparatedInteger(models.Model):
+ field = models.CommaSeparatedIntegerField(max_length=20)
+
+ def __unicode__(self):
+ return self.field
+
__test__ = {'API_TESTS': """
>>> from django import forms
>>> from django.forms.models import ModelForm, model_to_dict
@@ -1050,4 +1056,30 @@ def __unicode__(self):
<link href="/some/form/css" type="text/css" media="all" rel="stylesheet" />
<script type="text/javascript" src="/some/form/javascript"></script>
+>>> class CommaSeparatedIntegerForm(ModelForm):
+... class Meta:
+... model = CommaSeparatedInteger
+
+>>> f = CommaSeparatedIntegerForm().fields['field']
+>>> f.clean('1,2,3')
+u'1,2,3'
+>>> f.clean('1a,2')
+Traceback (most recent call last):
+...
+ValidationError: [u'Enter only digits separated by commas.']
+>>> f.clean(',,,,')
+u',,,,'
+>>> f.clean('1.2')
+Traceback (most recent call last):
+...
+ValidationError: [u'Enter only digits separated by commas.']
+>>> f.clean('1,a,2')
+Traceback (most recent call last):
+...
+ValidationError: [u'Enter only digits separated by commas.']
+>>> f.clean('1,,2')
+u'1,,2'
+>>> f.clean('1')
+u'1'
+
"""}
Please sign in to comment.
Something went wrong with that request. Please try again.