Replies: 7 comments
|
There isn't an official way of doing it at the moment - it's possible to override the save method on a table, but a bit tricky. Having a proper API for it would be nice, as it's a much more common requirement than I expected. |
|
Looking forward to that then. |
|
A code example showing how to override the save method on a table will be good. I can't see how to that. |
|
It's something like this: from piccolo.utils.sync import run_sync
def save(self, columns=None):
save_ = super().save
class Save:
async def run(self, *args, **kwargs):
# Add custom logic here
await save_(columns=columns).run(*args, **kwargs)
def run_sync(self, *args, **kwargs):
return run_sync(self.run(*args, **kwargs))
def __await__(self):
return self.run().__await__()
return Save()The reason it looks a bit complicated is because Piccolo lets you run a query as sync or async. |
|
Thanks a lot. I observed that the run method is called when adding records. But doesn't seem to be called when updating existing records. |
|
@Fidel-C You can also try something like this. It is based on the @dantownsend code with minor modifications to use (override) also |
Uh oh!
There was an error while loading. Please reload this page.
Just wondering whether Hooks can be emplemented externally in any part of the app. Something like Django signals. So that wherever a record changes or is added in the database, even if the action was called from the admin panel, the Hook would still be triggered . I think this restriction to PiccoloCrud alone is very limiting. I wish there could be a Django-like approach to this.
All reactions