#9148: Unable to change widget when inheriting models.DateTimeField
----------------------------------------------------------------+-----------
Reporter: nanaki | Owner:
nobody
Status: new | Milestone:
Component: django.contrib.admin | Version:
1.0
Keywords: DateTimeField DateField inheritance inherit widget | Stage:
Unreviewed
Has_patch: 0 |
----------------------------------------------------------------+-----------
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()
}}}
--
Ticket URL: <http://code.djangoproject.com/ticket/9148>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---