Skip to content

Hooks Outside PiccoloCrud #293

Closed
Closed

Description

@Fidel-C

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.

Activity

  1. dantownsend commented on Aug 22, 2024

    @dantownsend
    Member

    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.

  2. Fidel-C commented on Aug 22, 2024

    @Fidel-C
    Author

    Looking forward to that then.

  3. Fidel-C commented on Sep 26, 2024

    @Fidel-C
    Author

    A code example showing how to override the save method on a table will be good. I can't see how to that.

  4. dantownsend commented on Sep 26, 2024

    @dantownsend
    Member

    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.

  5. Fidel-C commented on Sep 27, 2024

    @Fidel-C
    Author

    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.
    Quite complicated, just like you said.

  6. sinisaos commented on Sep 27, 2024

    @sinisaos
    Member

    @Fidel-C You can also try something like this. It is based on the @dantownsend code with minor modifications to use (override) also save (both save and update) and remove methods.

  7. Fidel-C commented on Sep 28, 2024

    @Fidel-C
    Author

    @sinisaos Thanks a lot.

  8. converted this issue into a discussion #316 on Aug 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions