Replies: 14 comments
-
|
@coder3112 It's a nice idea. What kind of things would you run in the functions though? |
Beta Was this translation helpful? Give feedback.
-
|
These would fundamentally do the same things as Example:class User(Table, tablename='my_users'):
username = Varchar(length=100, unique=True)
password = Secret(length=255)
first_name = Varchar(null=True)
last_name = Varchar(null=True)
email = Varchar(length=255, unique=True)
active = Boolean(default=False)
admin = Boolean(default=False)
class Config:
on_update = ['func']
class Profile(Table, tablename='foo'):
uuid = UUID()
user = ForeignKey(references=User)
active = Boolean(default=True)
def func():
# Update the value of Profile.active if it has changed.
Now if I update |
Beta Was this translation helpful? Give feedback.
-
|
@coder3112 OK, I can see this being useful. I guess that by having these functions, it's cleaner than overriding the I can imagine people putting some validation logic in there. The limitation with the Django implementation is that it doesn't trigger for bulk updates. The same would have to be true here. One thing to be aware of is Piccolo doesn't currently use a class User(Table, tablename='my_users', pre_save=[some_function]):
username = Varchar(length=100, unique=True)
password = Secret(length=255)
first_name = Varchar(null=True)
last_name = Varchar(null=True)
email = Varchar(length=255, unique=True)
active = Boolean(default=False)
admin = Boolean(default=False) |
Beta Was this translation helpful? Give feedback.
-
|
Ok, so I can start working on a PR now. I guess it would change the |
Beta Was this translation helpful? Give feedback.
-
|
@coder3112 Yeah, sounds good - thanks. |
Beta Was this translation helpful? Give feedback.
-
|
@coder3112 You should take a look at what has been implemented in Ormar - They have a very nice concept called "signals" and you can even create your own. Being able to change fields "pre_update", like dates or passwords would be some of my use cases, and "post_delete" for scheduling cleanup jobs. |
Beta Was this translation helpful? Give feedback.
-
|
@davidolrik Maybe it's better to turn this into a discussion. I think one workaround to is to implement the signals inside the model itself so it's more contained within the model class, and they I think that can help with better type hints. These methods would be know to piccolo and will be treated as signals. |
Beta Was this translation helpful? Give feedback.
-
|
Any idea by when this would be implemented? |
Beta Was this translation helpful? Give feedback.
-
|
@devsarvesh92 It's still a work in progress. As @aminalaee mentions, it can sometimes be an anti-pattern and be abused. On the other hand, this topic has come up a few times, so there's a desire for it. For a client project I did this (overriding the class MyTable(Table):
def save(self, columns=None):
instance = self
save_ = super().save
class Save:
async def run(self, *args, **kwargs):
# do custom logic before saving
# if instance.some_value ....
await save_(columns=columns).run(*args, **kwargs)
# do custom logic after saving
# if instance.some_value ....
def run_sync(self, *args, **kwargs):
return run_sync(self.run(*args, **kwargs))
def __await__(self):
return self.run().__await__()
return Save()It looks a bit complex, but that's because Piccolo lets you run a query async or sync. We have hooks in Piccolo Admin which is OK if you just want it for Piccolo Admin. It wouldn't be that hard for us to let the user specify |
Beta Was this translation helpful? Give feedback.
-
|
Thanks @dantownsend 👍 |
Beta Was this translation helpful? Give feedback.
-
|
is this feature out yet? |
Beta Was this translation helpful? Give feedback.
-
|
any updates? @dantownsend @sinisaos |
Beta Was this translation helpful? Give feedback.
-
|
@hoosnick There has been no progress on this issue since PR #83 was closed. Since I don't know what the API should be (add these methods to the table meta or something else), I haven't looked into it. Feel free to make a PR if you want. As a workaround, you can override the methods (as @dantownsend showed in previous comments) or you can override all methods this.You can also override methods in |
Beta Was this translation helpful? Give feedback.
-
|
yes the one example @dantownsend really works well i've done it like this. here's my basic implementations |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Issue
Currently when a schema is made, there is no way to run a function at time of creation. Although custom crud functions can be made, it would be better if some kind of functions ran at time of creation, deletion and update of the schemas through a pre_defined meta class.
Minimal Use Case
Here, I would want that a function
foo_func and foo_func2be executed when it is deleted, updated or created.Proposed solution
I can use a function like so:
Now whenever I run
INSERT,UPDATEorDELETE, these functions will run accordingly.PS - I can start working on a PR if you think this should be done
Beta Was this translation helpful? Give feedback.
All reactions