Ticket #8556: 8556-1.diff

File 8556-1.diff, 2.1 KB (added by Matt McClanahan, 16 years ago)

Add admin widget

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

     
    394394
    395395# TODO: Maybe move this into contrib, because it's specialized.
    396396class CommaSeparatedIntegerField(CharField):
    397     pass
     397    def formfield(self, **kwargs):
     398        defaults = {
     399            'form_class': forms.RegexField,
     400            'regex': '^[\d,]+$',
     401            'max_length': self.max_length,
     402        }
     403        defaults.update(kwargs)
     404        return super(CommaSeparatedIntegerField, self).formfield(**defaults)
    398405
    399406ansi_date_re = re.compile(r'^\d{4}-\d{1,2}-\d{1,2}$')
    400407
  • django/contrib/admin/options.py

     
    9494            kwargs['widget'] = widgets.AdminIntegerFieldWidget
    9595            return db_field.formfield(**kwargs)
    9696
     97        # For CommaSeparatedIntegerFields, add a custom CSS class.
     98        if isinstance(db_field, models.CommaSeparatedIntegerField):
     99            kwargs['widget'] = widgets.AdminCommaSeparatedIntegerFieldWidget
     100            return db_field.formfield(**kwargs)
     101
    97102        # For TextInputs, add a custom CSS class.
    98103        if isinstance(db_field, models.CharField):
    99104            kwargs['widget'] = widgets.AdminTextInputWidget
  • django/contrib/admin/widgets.py

     
    236236        if attrs is not None:
    237237            final_attrs.update(attrs)
    238238        super(AdminIntegerFieldWidget, self).__init__(attrs=final_attrs)
     239
     240class AdminCommaSeparatedIntegerFieldWidget(forms.TextInput):
     241    def __init__(self, attrs=None):
     242        final_attrs = {'class': 'vCommaSeparatedIntegerField'}
     243        if attrs is not None:
     244            final_attrs.update(attrs)
     245        super(AdminCommaSeparatedIntegerFieldWidget, self).__init__(attrs=final_attrs)
Back to Top