Python PeeWee IntegerField - Default value of unix timestamp
Question: I would like to manage timestamps in my database using seconds since epoch
using a PeeWee IntegerField rather than a DateTimeField. I know that the field
definition can take a default value using a callable, but I would need to do
something like:

    
    
    timestamp = IntegerField(default=int(datetime.datetime.now().strftime('%s')))
    

Which I suppose is incorrect since the default value should not actually
invoke the callable.

I also tried wrapping this in a function and passing that function as a
callable:

    
    
    def get_timestamp():
        return int(datetime.datetime.now().strftime('%s'))
    
    timestamp = IntegerField(default=get_timestamp)
    

However this did not work either.

Is there a way to accomplish this, or do I have to retro-fit my data to use
DateTimeFields instead?


Answer: I see you mentioned that you tried wrapping your function call, I'm surprised
that didn't work for you, because it appears to work fine for me:

    
    
    >>> from datetime import datetime
    >>> from peewee import *
    >>> db = SqliteDatabase(':memory:')
    >>> def get_ts():
    ...     return int(datetime.now().strftime('%s'))
    
    >>> get_ts()
    1448471382
    
    >>> class TestModel(Model):
    ...     ts = IntegerField(default=get_ts)
    ...     class Meta:
    ...         database = db
    
    >>> TestModel.create_table()
    >>> tm = TestModel.create()
    >>> tm.ts
    1448471421
    >>> ts_from_db = TestModel.get(TestModel.id == tm.id)
    >>> ts_from_db.ts
    1448471421
    

The reason you need to wrap it is because the default param takes a _callable_
, and if you pass just the result of a function call, it interprets it as just
another static value.

