sqlalchemy 中关系的另一端是什么类型而不创建对象?

发布于 2024-09-24 19:10:42 字数 1356 浏览 5 评论 0原文

如何获取应该位于关系另一端的类的名称?它是在创建关系时声明的。我想该信息应该在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忘年祭陌 2024-10-01 19:10:42

也许是一个更简洁的解决方案。

for prop in class_mapper(Shelf).iterate_properties:
    if isinstance(prop, sqlalchemy.orm.RelationshipProperty):
       print prop.mapper.class_

双向进行一对多和多对多工作

Maybe a little bit neater solution.

for prop in class_mapper(Shelf).iterate_properties:
    if isinstance(prop, sqlalchemy.orm.RelationshipProperty):
       print prop.mapper.class_

works with one-to-many and many-to-many in both directions

以可爱出名 2024-10-01 19:10:42

从 sqlalchemy 0.8 开始,这可以通过以下方式完成:

from sqlalchemy import inspect
inspect(Shelf).relationships['books'].mapper.class_

As of sqlalchemy 0.8 this can be done with:

from sqlalchemy import inspect
inspect(Shelf).relationships['books'].mapper.class_
烂柯人 2024-10-01 19:10:42

我找到了一种解决方案,但我不能 100% 确定它是否始终有效。要找到关系另一端所需的类(类型)

class_mapper(Shelf)._props['books'].mapper.class_

我们可能不知道属性的名称(例如 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

class_mapper(Shelf)._props['books'].mapper.class_

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文