MCPcopy Index your code
hub / github.com/fastapi/sqlmodel / __init__

Method __init__

sqlmodel/main.py:826–844  ·  view source on GitHub ↗
(__pydantic_self__, **data: Any)

Source from the content-addressed store, hash-verified

824 return new_object
825
826 def __init__(__pydantic_self__, **data: Any) -> None:
827 # Uses something other than `self` the first arg to allow "self" as a
828 # settable attribute
829
830 # SQLAlchemy does very dark black magic and modifies the __init__ method in
831 # sqlalchemy.orm.instrumentation._generate_init()
832 # so, to make SQLAlchemy work, it's needed to explicitly call __init__ to
833 # trigger all the SQLAlchemy logic, it doesn't work using cls.__new__, setting
834 # attributes obj.__dict__, etc. The __init__ method has to be called. But
835 # there are cases where calling all the default logic is not ideal, e.g.
836 # when calling Model.model_validate(), as the validation is done outside
837 # of instance creation.
838 # At the same time, __init__ is what users would normally call, by creating
839 # a new instance, which should have validation and all the default logic.
840 # So, to be able to set up the internal SQLAlchemy logic alone without
841 # executing the rest, and support things like Model.model_validate(), we
842 # use a contextvar to know if we should execute everything.
843 if finish_init.get():
844 sqlmodel_init(self=__pydantic_self__, data=data)
845
846 def __setattr__(self, name: str, value: Any) -> None:
847 if name in {"_sa_instance_state"}:

Callers 2

__init__Method · 0.45
__init__Method · 0.45

Calls 1

sqlmodel_initFunction · 0.85

Tested by

no test coverage detected