Issue #24 new

Auto Update Fields?

Randall Degges
created an issue

Hey again :)

I've got another suggestion for a feature I think would be useful, a special variable, updates, which would allow you to specify values for keys that are automatically generated whenever the model is saved (on update).

For instance:

class Login(DynamoDBModel):
    __table__ = u'logins_login'
    __hash_key__ = u'last_login'
    __schema__ = {
        u'last_login': unicode,
    }
    __updates__ = {
        u'last_login': lambda: datetime.now(utc_tz),
    }

This would be cool because it would make it so that instead of having to override the DynamoDBModel.save() method, users could define some 'onupdate' type functionality easily via the updates class attribute.

What do you guys think?

Comments (4)

  1. Randall Degges reporter

    We're using it for the typical 'updated' timestamp field, but I think it can also be useful in other casees, e.g.

    • Re-generate user statistics before every save(), then store the results.
    • Send of API requests to some other place if something changed before save().
    • etc.
  2. Éric Araujo

    Re-generate user statistics before every save(), then store the results.

    Hum I would expect this to be a job for a controller, not model.

    Send of API requests to some other place if something changed before save().

    Likewise, sending outbound requests (I assume API requests means some kind of HTTP requests) as side effect just before saving seems like a poor man’s event/signal system. I guess we haven’t had the need before our web framework of choice has a very good event system.

    etc.

    Do you have other examples? I hope i didn’t sound too negative, but I’m +1 on automatically filled fields before save (“updated” field), but -0.5 on “unupdate” event system. Max Noel what’s your opinion?

  3. Log in to comment