python – SQLAlchemy: Call a method in another class with lazy=dynamic

python – SQLAlchemy: Call a method in another class with lazy=dynamic

It is quite hard to guess what you are trying to do as the snippet you presented is very limited. Also it is not really clear what exact relationship there is between model1 and model2.

Anyway, I guess you could do something like this (not tested):

from sqlalchemy.orm import object_session

class Model1(db.Model):
    model2 = db.relationship(Model2, lazy=dynamic)

    # call other_method
    @property
    def other_method(self):
        return object_session(self).query(Model2).
               with_parent(self).scalar().other_method()
 
class Model2(db.Model):
    def other_method(self):
    # method stuff

Depending on the relationship and the method itself, you might also have to adjust the query further.

python – SQLAlchemy: Call a method in another class with lazy=dynamic

Leave a Reply

Your email address will not be published.