如何获得该关系另一端的类别的名称? 它是在建立关系时宣布的。 我猜测,信息应当放在 s木图象中。
Let s say we have these three classes and a relations between them. Book * --- 1 Shelf and Book * --- * Author
class Shelf(Base):
__tablename__ = shelves
id = Column(Integer, primary_key = True)
name = Column(String)
#one-to-many books by backref in Book.shelf
def __init__(self, name=""):
self.name = name
class Book(Base):
__tablename__ = books
id = Column(Integer, primary_key = True)
title = Column(String)
#many-to-one shelf
shelf_id = Column(Integer, ForeignKey( shelves.id ))
shelf = relationship(Shelf, backref=backref( books , order_by=id))
def __init__(self, title=""):
self.title = title
author_book = Table( author_book , metadata,
Column( author_id , Integer, ForeignKey( authors.id )),
Column( book_id , Integer, ForeignKey( books.id ))
)
class Author(Base):
__tablename__ = authors
id = Column(Integer, primary_key = True)
name = Column(String)
#many-to-many books
books = relationship( Book , secondary=author_book, backref= authors )
def __init__(self, name=""):
self.name = name
From class_mapper(Class).iterate_properties we can easily get different properties: ColumnProperty and RelationshipProperty. There should be a way to get at leat the name of the class that is in relation. Any ideas?