Not sure if this is the correct way but it works:

>>> import datetime
>>> s = '2009-01-01'
>>> default = datetime.date(*(int(i) for i in s.split('-')))

It's easier to do from a tuple of int if possible:

>>> s = (2009,1,1)
>>> default = datetime.date(*s)

On Nov 20, 8:41 am, Dmitri Zagidulin <[email protected]> wrote:
> I'm using a form created with SQLFORM.factory as an edit form (it
> saves from and loads to session).
>
> If I have a field of type 'date', the value that it saves is in the
> usual ISO format, a string like '2009-01-01'.
> But if I then try to use that saved value as the default for the field
> in the factory constructor,
> such as
> Field('mydate', 'date', default='2009-01-01'),
> it gives me the following error:
> AttributeError: 'str' object has no attribute 'strftime'
>
> So, my first question is - wouldn't it be a good idea to let date
> fields take defaults as strings, for this exact usage scenario - a
> round trip of saving and loading?
>
> Ok, so I figured this meant that date fields expected python date
> objects as defaults.
> When I search for how to create date objects from string, most posts
> recommend using dateutil.parser.
> But that means installing another package on the server, something I'm
> reluctant to do unless absolutely necessary. And the framework must
> already have some way of parsing strings into dates, but what is it?
>
> So what's the correct way of using a date string to create a default
> for a date field?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py-users" 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/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to