English 中文(简体)
如果不制造物体, s酸chem在另一端的关系是什么?
原标题:What type is on the other end of relation in sqlalchemy without creating objects?

如何获得该关系另一端的类别的名称? 它是在建立关系时宣布的。 我猜测,信息应当放在 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?

最佳回答

也许没有什么次要的解决办法。

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

在两个方向与一对一对一对一对一对一对一对一对一对一。

问题回答

0.8 0.8

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

我找到了一种解决办法,但我并不相信,如果它始终发挥作用的话。 在关系的另一端找到一个类别(类型)

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

我们可能不了解属性名称(例如books in this case),但很难通过字典 * 寻找价值礼节,即sqlalchemy.orm.properties.Relationproperty





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签