"Automatic" datetime fields when creating a registry and updating a registry. #404
-
Is there any "automatic" methods to implement a quick and simple update in a Of course I can always set the field before all my updates, but can (somehow) ormar automatically handle this? Some ORMs has options in Best regards. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
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, "updated_date", datetime.now()) Note that this way you can do much more modifications as needed than just setting the date to now. |
Beta Was this translation helpful? Give feedback.
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.: