Howto: Default value for datetime field in plone5 without using plone.directives

IMO the best way to do this is to use defaultFactory in your schema:

start = schema.Date(
    title='Start',
    defaultFactory=datetime.datetime.today,
)

This has the advantage of not being tied to a particular form library, as well as being the most readable solution proposed. Dexterity's __getattr__ is aware of defaultFactory and will calculate the default if you try to fetch an attribute that doesn't have a value stored. (So beware; if you don't set a value then you'll get a new date if you access the default again tomorrow.)

You can also write context-dependent default factories; see https://pypi.python.org/pypi/zope.schema#id16

2 Likes