Hello all,

Can a @hybrid_property be overriden by a subclass?

Consider this modified example:
(from 
http://docs.sqlalchemy.org/en/rel_0_9/orm/extensions/hybrid.html#correlated-subquery-relationship-hybrid)

A subclass of User, UserWithBonuses, have its computed `balance` by adding 
all balances of all SavingAccounts plus the amounts of all bonuses.


from sqlalchemy import Column, Integer, ForeignKey, Numeric, Stringfrom 
sqlalchemy.orm import relationshipfrom sqlalchemy.ext.declarative import 
declarative_basefrom sqlalchemy.ext.hybrid import hybrid_propertyfrom 
sqlalchemy import select, func
Base = declarative_base()
class SavingsAccount(Base):
    __tablename__ = 'account'
    id = Column(Integer, primary_key=True)
    user_id = Column(Integer, ForeignKey('user.id'), nullable=False)
    balance = Column(Numeric(15, 5))
class User(Base):
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True)
    name = Column(String(100), nullable=False)

    accounts = relationship("SavingsAccount", backref="owner")

    @hybrid_property
    def balance(self):
        return sum(acc.balance for acc in self.accounts)

    @balance.expression
    def balance(cls):
        return select([func.sum(SavingsAccount.balance)]).\
                where(SavingsAccount.user_id==cls.id).\
                label('total_balance')




class UserWithBonuses(Base):
    __tablename__ = 'user_with_bonuses'
    id = Column(Integer, primary_key=True)

    bonuses = relationship("Bonus", backref="owner")

    @hybrid_property
    def balance(self):
        return sum(acc.balance for acc in self.accounts) + sum(bonus.amount for 
bonus in self.bonuses)


    @balance.expression
    def balance(cls):
        return select([func.sum(SavingsAccount.balance)]).\
                where(SavingsAccount.user_id==cls.id).\
                label('total_balance') ... 

# A corresponding sum of bonuses `select` would be added here



-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to