Open Kyzegs opened 2 years ago
The post_save
decorator takes the model class you want to define the signal for as an argument. This cannot be accessed during the class definition. You could get tricky with partial functions and own custom decorators to get this to work at runtime, or you could simply call the signal-creator after the class definition:
from tortoise.backends.base.client import BaseDBAsyncClient
from tortoise.signals import post_save, Signals
from typing import List, Optional, Type
class Example:
async def signal_post_save(
self,
sender: Type[Model],
instance: Model,
created: bool,
using_db: Optional[BaseDBAsyncClient],
update_fields: List[str],
) -> None:
print(sender, instance, using_db, created, update_fields)
Example.register_listener(Signals.post_save, Example.signal_post_save)
Although far from elegant, you get to keep the signal definition in the class.
Describe the bug You're unable to use signal decorators within classes.
To Reproduce