tortoise / tortoise/tortoise-orm
Question about signals in model inheritance
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.6k
- Forks
- 516
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 9
Description
Is your feature request related to a problem? Please describe.
I want to still be able to use signals from the parent class when inheriting
I have the following code
from tortoise import fields
from tortoise.models import Model
from tortoise.contrib.pydantic import pydantic_model_creator
from tortoise.signals import post_delete, post_save, pre_delete, pre_save
class BaseModel(Model):
id = fields.IntField(pk=True)
created_at = fields.DatetimeField(auto_now_add=True)
create_by = fields.TextField(max_length=200,null=True)
update_at = fields.DatetimeField(auto_now=True)
update_by = fields.TextField(max_length=200,null=True)
class Meta:
abstract = True
@pre_save(BaseModel)
async def base_model_pre_save(
sender: "Type[BaseModel]", instance: BaseModel, using_db, update_fields
) -> None:
print("BaseModel some logic")
class Users(BaseModel):
user_id = fields.CharField(max_length=20)
auth_type = fields.IntField(default=1)
first_name = fields.CharField(max_length=50, null=True)
last_name = fields.CharField(max_length=50, null=True)
password = fields.CharField(max_length=50)
mail = fields.CharField(max_length=50,null=True)
class Meta:
unique_together=(("id", "user_id", "auth_type"))
User_R = pydantic_model_creator(Users, name="User_R")
User_I = pydantic_model_creator(Users, name="User_I", exclude_readonly=True)
@pre_save(Users)
async def users_model_pre_save(
sender: "Type[Users]", instance: Users, using_db, update_fields
) -> None:
print("UsersModel some logic")
Describe the solution you'd like
I want to be able to print the following when I run Users.create
BaseModel some logic
UsersModel some logic
But now it only prints one line of results
UsersModel some logic
Can you help me with this problem? Or have some alternatives, then I would be very grateful!
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the example using the BaseModel and Users pre_save handlers, then call Users.create and observe which handler runs. Confirm that done means both “BaseModel some logic” and “UsersModel some logic” are printed, and document or test the resulting inheritance behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- database
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100