Sqlalchemy: Initializing Attribute Based On Other Attribute
I'm working on a Python project using SQLAlchemy. I have following class (I have omitted some methods irrelevant to the question): class Cmd(Base):     __tablename__ = 'commands'
Solution 1:
You can use the @reconstructor decorator:
from sqlalchemy.orm import reconstructor
classCmd(Base):
    __tablename__ = "commands"
    dbid = Column(Integer, Sequence("commands_seq"), primary_key = True)
    cmd_id = Column(SmallInteger)
    instance_dbid =  Column(Integer, ForeignKey("instances.dbid"))
    type = Column(String(20))
    __mapper_args__ = {
    "polymorphic_on" : type,
    "polymorphic_identity" : "Cmd"
    }
    def__init__(self, cmd_id):
        self.cmd_id = cmd_id
        self.cmd_name = event_names[self.cmd_id]
    @reconstructordefinit_db_load(self):
        self.cmd_name = event_names[self.cmd_id]
See this doc under "Constructors and Object Initialization".
Post a Comment for "Sqlalchemy: Initializing Attribute Based On Other Attribute"