Форум сайта python.su
class Account(Base): __tablename__ = 'account' id = Column(Integer, primary_key=True) taxes = relationship('Tax', primaryjoin='and_(Account.id == Tax.account_id,' 'Tax.removed == False)', collection_class=attribute_mapped_collection('id'), cascade='all') def create_tax(self, id_, name): c = Tax(id=id_, name=name) self.taxes[c.id] = c def remove_tax(self, id_): contribution = self.taxes[id_] contribution.removed = True class Tax(Base): __tablename__ = 'tax' id = Column(Integer, primary_key=True) name = Column(String(50), nullable=False) account_id = Column(Integer, ForeignKey(Account.id), nullable=False) removed = Column(Boolean, nullable=False, default=False) Base.metadata.drop_all(engine) Base.metadata.create_all(engine) if __name__ == '__main__': account = Account(id=1) account.create_tax(id_=1, name='General tax') account.create_tax(id_=2, name='Selective tax') assert len(account.taxes) == 2 account.remove_tax(id_=1) assert len(account.taxes) == 1
Офлайн