Code

Opened 6 years ago

Closed 5 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: UI/UX:

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 gogna 5 years ago.

Download all attachments as: .zip

Change History (6)

comment:1 Changed 5 years ago by gregoire

  • Cc gregoire@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 Changed 5 years ago by gogna

  • Cc flavio.curella@… added

Changed 5 years ago by gogna

comment:3 Changed 5 years ago by gogna

  • Has patch set
  • Needs tests set
  • Summary changed from Unable to change widget when inheriting models.DateTimeField to Admin enforces default widgets, preventing the use of custom ones

comment:4 Changed 5 years ago by jacob

The current patch on #8306 fixes this also.

comment:5 Changed 5 years ago by jacob

  • Resolution set to fixed
  • Status changed from new to closed

(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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.