tortoise / tortoise/tortoise-orm

Correct way to override model constructor

Open
#1,242 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

Hello. Apologies if I overlooked something but I have combed the documentation trying to figure out a working technique for overriding the instance __init__/constructor method (i.e. to set up some non-db-backed instance vars etc, after the instance is initialized - whether when being pulled out of the db or from direct instantiation). I tried overriding __init__ (as it is defined with the signature here) and, calling super.__init__ at the top of the overriding method body, but does not seem to ever get called. I may be missing something. If it helps any to clarify I'm either looking for how to do this override directly or if there is a callback etc (something like the equivalent of Rails/ActiveRecord's after_initialize callback). Thank you 🙏

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with tortoise/models.py around the init definition linked in the issue, then trace how model instances are constructed for database loading and direct instantiation. Compare the supported lifecycle behavior with the documentation; done means documenting the correct override technique or explicitly explaining whether an initialization callback exists.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
database
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.