sqlalchemy 中关系的另一端是什么类型而不创建对象?
如何获取应该位于关系另一端的类的名称?它是在创建关系时声明的。我想该信息应该在 sqlalchemy.orm.util.class_mapper 中的某个位置
假设我们有这三个类以及它们之间的关系。 书 * --- 1 个书架和 Book * --- * 作者
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
从class_mapper(Class).iterate_properties我们可以很容易地得到不同的属性:ColumnProperty和RelationshipProperty。 应该有一种方法至少可以获取相关类的名称。有什么想法吗?
How to obtain a name of the class that should be on the other end of the relation? It was declared when creating relationship. I guess that information should be somwhere in sqlalchemy.orm.util.class_mapper
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许是一个更简洁的解决方案。
双向进行一对多和多对多工作
Maybe a little bit neater solution.
works with one-to-many and many-to-many in both directions
从 sqlalchemy 0.8 开始,这可以通过以下方式完成:
As of sqlalchemy 0.8 this can be done with:
我找到了一种解决方案,但我不能 100% 确定它是否始终有效。要找到关系另一端所需的类(类型)
我们可能不知道属性的名称(例如 books 在这种情况下),但是通过字典 *_props* 寻找 sqlalchemy.orm.properties.RelationshipProperty 实例的值并不麻烦
I found a sort of solution, but I'm not 100% sure if it works always.To find a class (type ) expected on the other end of relation
We may not know the names of attributes (like books in this case), but it isn't much trouble to go through dictionary *_props* with looking for values witch are instances of sqlalchemy.orm.properties.RelationshipProperty