Opened 16 years ago
Closed 16 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)
Change History (6)
comment:1 by , 16 years ago
Cc: | added |
---|
comment:2 by , 16 years ago
Cc: | added |
---|
by , 16 years ago
Attachment: | custom_widget.diff added |
---|
comment:3 by , 16 years ago
Has patch: | set |
---|---|
Needs tests: | set |
Summary: | Unable to change widget when inheriting models.DateTimeField → Admin enforces default widgets, preventing the use of custom ones |
comment:5 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | new → 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 aroundrequest
so that you can easily modify fields based on request (as in #3987).
Thanks to James Bennet for the original patch; Alex Gaynor and Brian Rosner also contributed.
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.