Opened 16 years ago

Closed 15 years ago

#9148 closed (fixed)

Admin enforces default widgets, preventing the use of custom ones

Reported by: nanaki Owned by: nobody
Component: contrib.admin Version: 1.0
Severity: Keywords: DateTimeField DateField inheritance inherit widget
Cc: gregoire@…, flavio.curella@… Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When you inherit models.DateTimeField (and several other fields) you are unable to change its widget it always uses the default.
The code in django/contrib/admin/options.py (line 68) looks like this:
It forcibly overrides anything you put in widget or form_class.

        if isinstance(db_field, models.DateTimeField):
            kwargs['form_class'] = forms.SplitDateTimeField
            kwargs['widget'] = widgets.AdminSplitDateTime()
            """
            return db_field.formfield(**kwargs)

I tried changing it to this:
But then all my DateTime fields lost their widgets.

        if isinstance(db_field, models.DateTimeField):
            defaults = {'form_class': forms.SplitDateTimeField,
                        'widget': widgets.AdminSplitDateTime()}
            defaults.update(kwargs)
            return db_field.formfield(**kwargs)

This is a simplified example

from django.db import models
from django.forms import Field,DateTimeField
from django.forms.widgets import Widget

from django.utils.safestring import mark_safe

class ReadonlyWidget(Widget):
    def render(self, name, value, attrs=None):
        return mark_safe(u'<span>%s</span>' % value)

class ReadonlyDateTimeField(DateTimeField):
    def formfield(self, **kwargs):
        defaults = {'form_class': Field, 'widget': ReadonlyWidget}
        defaults.update(kwargs)
        return super(ReadonlyDateTimeField, self).formfield(**defaults)

class Ticket(models.Model):
    date_raised = ReadonlyDateTimeField()
    date_closed = models.DateTimeField()

Attachments (1)

custom_widget.diff (4.0 KB ) - added by Flavio Curella 15 years ago.

Download all attachments as: .zip

Change History (6)

comment:1 by gregoire, 16 years ago

Cc: gregoire@… added

It is the same problem for every widget specific to the admin.

I wanted to use it with TextField to get a RichTextField with TinyMCE enabled, but I can't. I have to inherit from CharField and reimplement TextField to make it work.

comment:2 by Flavio Curella, 15 years ago

Cc: flavio.curella@… added

by Flavio Curella, 15 years ago

Attachment: custom_widget.diff added

comment:3 by Flavio Curella, 15 years ago

Has patch: set
Needs tests: set
Summary: Unable to change widget when inheriting models.DateTimeFieldAdmin enforces default widgets, preventing the use of custom ones

comment:4 by Jacob, 15 years ago

The current patch on #8306 fixes this also.

comment:5 by Jacob, 15 years ago

Resolution: fixed
Status: newclosed

(In [9760]) Cleaned up and refactored ModelAdmin.formfield_for_dbfield:

  • The new method uses an admin configuration option (formfield_overrides); this makes custom admin widgets especially easy.
  • Refactored what was left of formfield_for_dbfield into a handful of smaller methods so that it's easier to hook in and return custom fields where needed.
  • These formfield_for_* methods now pass around request so that you can easily modify fields based on request (as in #3987).

Fixes #8306, #3987, #9148.

Thanks to James Bennet for the original patch; Alex Gaynor and Brian Rosner also contributed.

Note: See TracTickets for help on using tickets.
Back to Top