Skip to content

"Automatic" datetime fields when creating a registry and updating a registry. #404

Answered by collerek
igormorgado asked this question in Q&A
Discussion options

You must be logged in to vote

Since setting a date on the update is just one of the possible use cases ormar adopts a much more flexible (yet at the same time not so obvious as setting one param on a field) approach of signals.

You can register a signal function that will update your date just before save/update. You can register the same signal callback function for multiple models (in your use case for simplicity the column to update should have the same name). You can read more here: https://collerek.github.io/ormar/signals/

Sample signal can be i.e.:

# define a callback that auto-updates the update date
@pre_update([ModelA, ModelB ... ModelZ])
async def before_update(sender, instance, **kwargs):
    setattr(instance

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@igormorgado
Comment options

Answer selected by igormorgado
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants