easy create nested models for sqlalchemy
Project description
sqlalchemy-nest
sqlalchemy-nest is easy create nested models for sqlalchemy
Getting started
use declarative_nested_model_constructor
for declarative_base constructor
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.orm import declarative_base, relationship
from sqlalchemy_nest import declarative_nested_model_constructor
Base = declarative_base(constructor=declarative_nested_model_constructor)
class Root(Base):
__tablename__ = "root"
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String(100))
branches = relationship("Branch", back_populates="root", uselist=True, lazy="joined")
class Branch(Base):
__tablename__ = "branch"
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String(100))
root_id = Column(Integer, ForeignKey("root.id"))
root = relationship("Root")
initialization from kwargs.
sets attributes on the constructed instance using the names and values in kwargs.
root = {
'name': 'root',
'branches': [
{
'name': 'branch',
},
]
}
>>> session.add(Root(**root))
>>> session.commit()
>>> added_root: Root = session.query(Root).filter(Root.id == 1).first()
Root(id=1, name='root', branches=[Branch(id=1, name='branch', root_id=1)])
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
sqlalchemy-nest-1.0.0.tar.gz
(4.7 kB
view hashes)
Built Distribution
Close
Hashes for sqlalchemy_nest-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8e561a078020f9e0657f63884cde48c1179ced585f12db7724cf3f250b24c198 |
|
MD5 | af5b20902b9c94b5569dade62b6e25d2 |
|
BLAKE2b-256 | c3aa7cf2b5f2d2ab411aac24b2881725d631c3ca85f52dfd5b723cbd5af53a0c |