Ticket #8306: admin_field_overrides.diff

File admin_field_overrides.diff, 3.7 KB (added by James Bennett, 16 years ago)
  • django/contrib/admin/options.py

     
    125125    radio_fields = {}
    126126    prepopulated_fields = {}
    127127
     128    field_type_overrides = { models.DateTimeField: { 'form_class': forms.SplitDateTimeField,
     129                                                     'widget': widgets.AdminSplitDateTime },
     130                             models.DateField: { 'widget': widgets.AdminDateWidget },
     131                             models.TimeField: { 'widget': widgets.AdminTimeWidget },
     132                             models.TextField: { 'widget': widgets.AdminTextareaWidget },
     133                             models.URLField: { 'widget': widgets.AdminURLFieldWidget },
     134                             models.IntegerField: { 'widget': widgets.AdminIntegerFieldWidget },
     135                             models.CharField: { 'widget': widgets.AdminTextInputWidget },
     136                             models.ImageField: { 'widget': widgets.AdminFileWidget },
     137                             models.FileField: { 'widget': widgets.AdminFileWidget },
     138                             }
     139
    128140    def formfield_for_dbfield(self, db_field, **kwargs):
    129141        """
    130142        Hook for specifying the form Field instance for a given database Field
     
    150162                # Otherwise, use the default select widget.
    151163                return db_field.formfield(**kwargs)
    152164
    153         # For DateTimeFields, use a special field and widget.
    154         if isinstance(db_field, models.DateTimeField):
    155             kwargs['form_class'] = forms.SplitDateTimeField
    156             kwargs['widget'] = widgets.AdminSplitDateTime()
    157             return db_field.formfield(**kwargs)
     165        if db_field.__class__ in self.field_type_overrides:
     166            return dbfield.formfield(**dict(kwargs, **self.field_type_overrides[db_field.__class__]))
    158167
    159         # For DateFields, add a custom CSS class.
    160         if isinstance(db_field, models.DateField):
    161             kwargs['widget'] = widgets.AdminDateWidget
    162             return db_field.formfield(**kwargs)
    163 
    164         # For TimeFields, add a custom CSS class.
    165         if isinstance(db_field, models.TimeField):
    166             kwargs['widget'] = widgets.AdminTimeWidget
    167             return db_field.formfield(**kwargs)
    168        
    169         # For TextFields, add a custom CSS class.
    170         if isinstance(db_field, models.TextField):
    171             kwargs['widget'] = widgets.AdminTextareaWidget
    172             return db_field.formfield(**kwargs)
    173        
    174         # For URLFields, add a custom CSS class.
    175         if isinstance(db_field, models.URLField):
    176             kwargs['widget'] = widgets.AdminURLFieldWidget
    177             return db_field.formfield(**kwargs)
    178        
    179         # For IntegerFields, add a custom CSS class.
    180         if isinstance(db_field, models.IntegerField):
    181             kwargs['widget'] = widgets.AdminIntegerFieldWidget
    182             return db_field.formfield(**kwargs)
    183 
    184         # For TextInputs, add a custom CSS class.
    185         if isinstance(db_field, models.CharField):
    186             kwargs['widget'] = widgets.AdminTextInputWidget
    187             return db_field.formfield(**kwargs)
    188    
    189         # For FileFields and ImageFields add a link to the current file.
    190         if isinstance(db_field, models.ImageField) or isinstance(db_field, models.FileField):
    191             kwargs['widget'] = widgets.AdminFileWidget
    192             return db_field.formfield(**kwargs)
    193 
    194168        # For ForeignKey or ManyToManyFields, use a special widget.
    195169        if isinstance(db_field, (models.ForeignKey, models.ManyToManyField)):
    196170            if isinstance(db_field, models.ForeignKey) and db_field.name in self.raw_id_fields:
Back to Top