Trying to understand the right way to define a hybridproperty.expression that references an 'sub-attribute' of a relationship attribute. I have done it in another case by using the class name of the relationship attribute (Tire.weight vs. cls.tire.weight) and that's worked OK. But it begs the question how to do it if that class (Tire) is used in multiple relationship attributes. How does the .expression discern between the two.
Hopefully the example below explains better than my words probably have. See the final .expression "weight_of_tires"... how to make it reference the front_tire attribute and rear_tire attribute - both of the same model class - distinctly? This sample is using flask-alchemy - hopefully that doesn't confuse things.
class Tire(db.Model):
__tablename__ = 'tire'
id = Column(Integer, primary_key=True)
weight = Column(Integer)
size = Column(VARCHAR(50))
class Motorcycle(db.Model):
__tablename__ = 'motorcycle'
id = Column(Integer, primary_key=True)
front_tire_id = Column(ForeignKey(Tire.id))
rear_tire_id = Column(ForeignKey(Tire.id))
front_tire = relationship(Tire, foreign_keys=[front_tire_id], lazy='joined')
rear_tire = relationship(Tire, foreign_keys=[rear_tire_id], lazy='joined')
@hybrid_property
def weight_of_tires(self):
return self.front_tire.weight + self.rear_tire.weight
@weight_of_tires.expression
def weight_of_tires(cls):
# return Tire.weight + Tire.weight
# return cls.front_tire.weight + cls.rear_tire.weight