SQLAlchemy: get relationships from a db.Model

前端 未结 3 1475
星月不相逢
星月不相逢 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 21:06

    You just need to use the inspect module from sqlalchemy

    from sqlalchemy import inspect
    
    i = inspect(model)
    i.relationships
    

    If you need the class of each referred model you can do:

    referred_classes = [r.mapper.class_ for r in i.relationships]
    

提交回复
热议问题