ponyorm / pony

Pony Object Relational Mapper
Apache License 2.0
3.63k stars 245 forks source link

[FR] Add __post_init__ equivalent #689

Open Zocker1999NET opened 1 year ago

Zocker1999NET commented 1 year ago

It would be great to have something for Pony entities which works similar to the __post_init__ method for dataclasses, allowing to modify or further set up an entity object directly after it was created, without requiring to override Pony’s `init and then call it again with the same arguments.

This should be relatively easy to implement. However, while looking through Pony’s code, I don’t seem to get where this call should happen.

JoshYuJump commented 11 months ago

Maybe you can take a look at hooks https://docs.ponyorm.org/api_reference.html#entity-hooks

Zocker1999NET commented 2 months ago

The entity hooks were not helpful in my case as those (if I understand them correctly) trigger when interacting with the database. However, I wanted to adjust the defaults of some properties depending on other, given attributes & I would prefer if those dynamic defaults would already be applied before the object is stored into the DB.

(I just saw your suggestion again & thought I could answer it through I do not need a __post_init__ anymore but I think this might still be helpful.)