mirror of
https://github.com/django/django.git
synced 2025-07-05 02:09:13 +00:00
newforms-admin: DateTimeFields are now properly split into two widgets in the admin interface, using the new SplitDateTimeField and SplitDateTimeWidget
git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@4405 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
89f1f7bf24
commit
18ad7d6f8f
@ -207,6 +207,10 @@ class ModelAdmin(object):
|
|||||||
if isinstance(db_field, models.ManyToManyField) and db_field.rel.filter_interface:
|
if isinstance(db_field, models.ManyToManyField) and db_field.rel.filter_interface:
|
||||||
widget = widgets.FilteredSelectMultiple(db_field.verbose_name, db_field.rel.filter_interface-1)
|
widget = widgets.FilteredSelectMultiple(db_field.verbose_name, db_field.rel.filter_interface-1)
|
||||||
return db_field.formfield(widget=widget, **kwargs)
|
return db_field.formfield(widget=widget, **kwargs)
|
||||||
|
# For DateTimeFields, use a special field and widget.
|
||||||
|
if isinstance(db_field, models.DateTimeField):
|
||||||
|
return forms.SplitDateTimeField(required=not db_field.blank,
|
||||||
|
widget=widgets.AdminSplitDateTime(), label=capfirst(db_field.verbose_name), **kwargs)
|
||||||
return db_field.formfield(**kwargs)
|
return db_field.formfield(**kwargs)
|
||||||
|
|
||||||
def has_add_permission(self, request):
|
def has_add_permission(self, request):
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
{% load i18n %}
|
|
||||||
<p class="datetime">
|
|
||||||
{% trans "Date:" %} {{ bound_field.form_fields.0 }}<br />
|
|
||||||
{% trans "Time:" %} {{ bound_field.form_fields.1 }}
|
|
||||||
</p>
|
|
@ -3,6 +3,7 @@ Form Widget classes specific to the Django admin site.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from django import newforms as forms
|
from django import newforms as forms
|
||||||
|
import datetime
|
||||||
|
|
||||||
class FilteredSelectMultiple(forms.SelectMultiple):
|
class FilteredSelectMultiple(forms.SelectMultiple):
|
||||||
"""
|
"""
|
||||||
@ -19,9 +20,24 @@ class FilteredSelectMultiple(forms.SelectMultiple):
|
|||||||
def render(self, name, value, attrs=None, choices=()):
|
def render(self, name, value, attrs=None, choices=()):
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
output = [super(FilteredSelectMultiple, self).render(name, value, attrs, choices)]
|
output = [super(FilteredSelectMultiple, self).render(name, value, attrs, choices)]
|
||||||
output.append('<script type="text/javascript">addEvent(window, "load", function(e) {')
|
output.append(u'<script type="text/javascript">addEvent(window, "load", function(e) {')
|
||||||
# TODO: "id_" is hard-coded here. This should instead use the correct
|
# TODO: "id_" is hard-coded here. This should instead use the correct
|
||||||
# API to determine the ID dynamically.
|
# API to determine the ID dynamically.
|
||||||
output.append('SelectFilter.init("id_%s", "%s", %s, "%s"); });</script>\n' % \
|
output.append(u'SelectFilter.init("id_%s", "%s", %s, "%s"); });</script>\n' % \
|
||||||
(name, self.verbose_name.replace('"', '\\"'), int(self.is_stacked), settings.ADMIN_MEDIA_PREFIX))
|
(name, self.verbose_name.replace('"', '\\"'), int(self.is_stacked), settings.ADMIN_MEDIA_PREFIX))
|
||||||
return ''.join(output)
|
return u''.join(output)
|
||||||
|
|
||||||
|
class AdminSplitDateTime(forms.SplitDateTimeWidget):
|
||||||
|
"""
|
||||||
|
A SplitDateTime Widget that has some admin-specific styling.
|
||||||
|
"""
|
||||||
|
def __init__(self, attrs=None):
|
||||||
|
widgets = [forms.TextInput(attrs={'class': 'vDateField', 'size': '10'}),
|
||||||
|
forms.TextInput(attrs={'class': 'vTimeField', 'size': '8'})]
|
||||||
|
# Note that we're calling MultiWidget, not SplitDateTimeWidget, because
|
||||||
|
# we want to define widgets.
|
||||||
|
forms.MultiWidget.__init__(self, widgets, attrs)
|
||||||
|
|
||||||
|
def format_output(self, rendered_widgets):
|
||||||
|
return u'<p class="datetime">%s %s<br />%s %s</p>' % \
|
||||||
|
(_('Date:'), rendered_widgets[0], _('Time:'), rendered_widgets[1])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user