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

2014-03-04 Thread Peter Bengtsson


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 
>  
> 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. 
 

>
> 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 
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/f3831069-5e8b-45bf-8faf-c522310e7d46%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


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

2014-03-04 Thread Tom Evans
On Tue, Mar 4, 2014 at 10:45 PM, Peter Bengtsson  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

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFHbX1%2B4d8tpizusgRcKEdmNZBd71tvfE6k3xfKEzhHiQwH_TQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


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

2014-03-04 Thread Peter Bengtsson


On Tuesday, March 4, 2014 12:47:03 PM UTC-8, Tom Evans wrote:
>
> On Tue, Mar 4, 2014 at 8:27 PM, Peter Bengtsson 
>  
> wrote: 
> > 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. 
>
> What version of django are you using? 
>
> The link was to django master. So it's in all versions. 

 

> The code linked indicates quite clea 
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/81ccd7ce-d138-47e1-97ee-87742ac5cb42%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


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

2014-03-04 Thread Tom Evans
On Tue, Mar 4, 2014 at 8:27 PM, Peter Bengtsson  wrote:
> 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.
>

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFHbX1J17HmqHV60_jHCeyuaKiCxJ5%3DiNUQcGsu1XSHK6r%2BAVA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


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

2014-03-04 Thread Tom Evans
On Tue, Mar 4, 2014 at 8:27 PM, Peter Bengtsson  wrote:
> 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.

What version of django are you using?

The code linked indicates quite clea

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFHbX1JQLyrSk872PJJBNvBe0oVzi%3DPc3GN-YN_4_x%3DnHywzhg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


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

2014-03-04 Thread Peter Bengtsson
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

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/a5ddd8ee-b72e-46c8-9d3c-c211aa76fa25%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.