Why doesn't a forms.fields.DateField always return a datetime.date instance?

62 views
Skip to first unread message

Peter Bengtsson

unread,
Mar 4, 2014, 3:27:43 PM3/4/14
to django...@googlegroups.com
I've been googling for an explanation but nothing's come up.

See
https://github.com/django/django/blob/master/django/forms/fields.py#L447

If you use a `DateField` in your form, don't you expect it to produce a `datetime.date` instance?
E.g.

class MyForm:
    start = forms.DateField()

    def clean_start(self):
         if self.cleaned_data['start'] > datetime.date.today():
            raise ValidationError('too futuristic')
         return self.cleaned_data['start']

This won't work! You'll get a TypeError if you run that code.
Why is that?

It'd be easy to fix but because it's so blatant I just suspect I missed something obvious.


Peter

Tom Evans

unread,
Mar 4, 2014, 3:47:03 PM3/4/14
to django...@googlegroups.com
What version of django are you using?

The code linked indicates quite clea

Tom Evans

unread,
Mar 4, 2014, 3:48:39 PM3/4/14
to django...@googlegroups.com
On Tue, Mar 4, 2014 at 8:27 PM, Peter Bengtsson <pet...@gmail.com> wrote:
What version of django are you using?

The code linked indicates quite clearly that it should generate a
datetime.date() instance, or raise a ValidationError.

(apologies for double send, tab+space=bad).

Cheers

Tom

Peter Bengtsson

unread,
Mar 4, 2014, 5:45:45 PM3/4/14
to django...@googlegroups.com, teva...@googlemail.com
The link was to django master. So it's in all versions. 

Tom Evans

unread,
Mar 4, 2014, 8:19:17 PM3/4/14
to django...@googlegroups.com
On Tue, Mar 4, 2014 at 10:45 PM, Peter Bengtsson <pet...@gmail.com> wrote:
> The link was to django master. So it's in all versions.
>

Well, that doesn't necessarily follow does it? It could have been
changed recently in trunk, say for 1.6 release. If you were using 1.5
it could be different. I haven't looked back, since you didn't
actually say what version you are using.

Trivially, in 1.6 at least, it works precisely as you expect:

>>> class A(forms.Form):
... s = forms.DateField()
... def clean_s(self):
... print repr(self.cleaned_data['s'])
... return self.cleaned_data['s']
...
>>> A(data={'s':'2012-12-25'}).is_valid()
datetime.date(2012, 12, 25)
True


Does this form also have a custom clean() method, is that doing
something silly with cleaned_data['start'] after the form field has
pythonized it and before your clean_start() method is called?

Cheers

Tom

Peter Bengtsson

unread,
Mar 4, 2014, 11:56:10 PM3/4/14
to django...@googlegroups.com, teva...@googlemail.com


On Tuesday, March 4, 2014 5:19:17 PM UTC-8, Tom Evans wrote:
On Tue, Mar 4, 2014 at 10:45 PM, Peter Bengtsson <pet...@gmail.com> wrote:
> The link was to django master. So it's in all versions.
>

Well, that doesn't necessarily follow does it? It could have been
changed recently in trunk, say for 1.6 release. If you were using 1.5
it could be different. I haven't looked back, since you didn't
actually say what version you are using.

Trivially, in 1.6 at least, it works precisely as you expect:

>>> class A(forms.Form):
...     s = forms.DateField()
...     def clean_s(self):
...         print repr(self.cleaned_data['s'])
...         return self.cleaned_data['s']
...
>>> A(data={'s':'2012-12-25'}).is_valid()
datetime.date(2012, 12, 25)
True

Right you are!
It's independent of version.
My actual code, where this question arose from, isn't using forms.Form as the base class. Instead a custom base class which clearly is causing some problem. 

I'm using BaseForm from this http://www.peterbe.com/plog/django-baseform
Clearly it's affecting it in some bad way. 
Reply all
Reply to author
Forward
0 new messages