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
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()
Instead of using inspect
you can also use
model.__mapper__.relationships
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]