Ticket #2723: booleanwidget_radio.diff

File booleanwidget_radio.diff, 1.8 KB (added by David Tulig, 17 years ago)

Adds a custom boolean field widget to the admin interface.

  • django/contrib/admin/options.py

     
    166166            kwargs['widget'] = widgets.AdminTimeWidget
    167167            return db_field.formfield(**kwargs)
    168168
     169        # For BooleanFields, use a special widget.
     170        if isinstance(db_field, models.BooleanField):
     171            kwargs['widget'] = widgets.AdminBooleanSelect
     172            return db_field.formfield(**kwargs)
     173
    169174        # For FileFields and ImageFields add a link to the current file.
    170175        if isinstance(db_field, models.ImageField) or isinstance(db_field, models.FileField):
    171176            kwargs['widget'] = widgets.AdminFileWidget
  • django/contrib/admin/widgets.py

     
    4646
    4747    def __init__(self, attrs={}):
    4848        super(AdminTimeWidget, self).__init__(attrs={'class': 'vTimeField', 'size': '8'})
     49
     50class AdminBooleanSelect(forms.RadioSelect):
     51    def __init__(self, attrs=None):
     52        choices = ((u'1', _('Yes')), (u'2', _('No')))
     53        super(AdminBooleanSelect, self).__init__(attrs, choices)
     54
     55    def render(self, name, value, attrs=None, choices=()):
     56        try:
     57            value = {True: u'1', False: u'2', u'1': u'1', u'2': u'2'}[value]
     58        except KeyError:
     59            value = u'2'
     60        return super(AdminBooleanSelect, self).render(name, value, attrs, choices)
     61
     62    def value_from_datadict(self, data, files, name):
     63        value = data.get(name, None)
     64        return {u'1': True, u'2': False, True: True, False: False}.get(value, None)
    4965   
    5066class AdminSplitDateTime(forms.SplitDateTimeWidget):
    5167    """
Back to Top