SQLAlchemy: get relationships from a db.Model

前端 未结 3 1467
星月不相逢
星月不相逢 2020-12-15 20:32

I need to get a list of a model\'s properties which are actually relationships (that is, they were created by relationship()).

Say I have a model

3条回答
  •  情书的邮戳
    2020-12-15 20:51

    There is indeed - take a look at sqlalchemy.inspection.inspect. Calling inspect on a mapped class (for example, your Thing class) will return a Mapper, which has a relationships attribute that is dict like:

    from sqlalchemy.inspection import inspect
    
    thing_relations = inspect(Thing).relationships.items()
    

提交回复
热议问题