#13390 closed (fixed)
Using AdminSplitDateTime and clean method in DateTimeField
Reported by: | vaxXxa | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | 1.2-beta |
Severity: | Keywords: | AdminSplitDateTime | |
Cc: | gregor@… | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
models.py
class Order(models.Model): delivery_time_start = models.DateTimeField('Delivery Start', blank=True, null=True) delivery_time_end = models.DateTimeField('Delivery End', blank=True, null=True)
forms.py
from django.contrib.admin import widgets class OrderForm(forms.ModelForm): class Meta: model = Order widgets = { 'delivery_time_start': widgets.AdminSplitDateTime(), 'delivery_time_end': widgets.AdminSplitDateTime(), }
When I try to leave field delivery_time_start and delivery_time_end empty, I have error: "Enter a valid date/time." cause clean method in DateTimeField get value - list of 2 empty string [u"", u""]:
def clean(self, value): """ Validates that the input can be converted to a datetime. Returns a Python datetime.datetime object. """ super(DateTimeField, self).clean(value) if value in EMPTY_VALUES: return None if isinstance(value, datetime.datetime): return value if isinstance(value, datetime.date): return datetime.datetime(value.year, value.month, value.day) if isinstance(value, list): # Input comes from a SplitDateTimeWidget, for example. So, it's two # components: date and time. if len(value) != 2: raise ValidationError(self.error_messages['invalid']) value = '%s %s' % tuple(value)
, but EMPTY_VALUES has only None and "".
So, solution is - EMPTY_VALUES must contains list of 2 empty string.
Attachments (3)
Change History (10)
by , 15 years ago
Attachment: | 13390.tests.patch added |
---|
comment:1 by , 15 years ago
Cc: | added |
---|
comment:2 by , 15 years ago
milestone: | 1.2 |
---|---|
Patch needs improvement: | set |
Triage Stage: | Unreviewed → Accepted |
Really, a SplitDateTimeField
should be used with the widget, but since this logic already exists in DateTimeField.clean
it should be fixed to work correctly.
(and for future reference, a single patch with both fix and tests is preferred)
The patch doesn't ensure validation for datetime fields which are required (and use a the multi widget). You'd need something like this in the patch:
if self.required: raise ValidationError(self.error_messages['required']) return None
I'm dropping the 1.2 milestone (that's only for bugs introduced by 1.2 features)
comment:3 by , 15 years ago
I added a few more assertions to the test to make sure that required fields are handled corretly. I think you're point is already covered with the code. The to_python method of DateTimeField returns None if the value is a list of two times which are empty values. So the clean method in Field.clean will get None if an empty value from SplitedDateTimeWidget is passed into the field and will handle the required attribute correctly.
comment:4 by , 15 years ago
Patch needs improvement: | unset |
---|---|
Triage Stage: | Accepted → Ready for checkin |
Right you are, was looking at 1.1 code (the clean
method in the initial description of the ticket).
comment:5 by , 15 years ago
So, I thought, that this patch:
if value[0] in validators.EMPTY_VALUES and value[1] in validators.EMPTY_VALUES: return None
will be integrated in Django 1.2 RC or Django 1.2. Will it?
comment:6 by , 14 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Failing test.