Skip to content

The Model's save on create #82

Answered by tarsil
edimedia asked this question in Q&A
May 13, 2024 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

Hello !

I want to know if there is way to call the save method when creating models with Role.query.create(**kwargs) ?

class Role(UUIDMixin):
     """User model"""
    name: str = fields.CharField(max_length=75)
   slug: str = fields.CharField(max_length=75, index=True, unique=True)
   description: str = fields.TextField(null=True)


   async def save(self, *args, **kwargs: Any) -> Coroutine[Any, Any, type[Model] | Any]:
       self.slug = slugify(self.name)
       return await super().save(*args, **kwargs)

Hello. When the create is called it does call the save() method as well, so I do believe you can actually do what you want there?

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@edimedia
Comment options

Answer selected by edimedia
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants