Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 炼金术内省_Python_Sqlalchemy_Introspection - Fatal编程技术网

Python 炼金术内省

Python 炼金术内省,python,sqlalchemy,introspection,Python,Sqlalchemy,Introspection,我试图做的是从SqlAlchemy实体定义中获取它的所有列(),确定它们的类型和约束,以便能够预验证、转换数据并向用户显示自定义表单 我怎么能反省呢 例如: class Person(Base): ''' Represents Person ''' __tablename__ = 'person' # Columns id = Column(String(8), primary_key=True, default=uid_gen)

我试图做的是从SqlAlchemy实体定义中获取它的所有列(),确定它们的类型和约束,以便能够预验证、转换数据并向用户显示自定义表单

我怎么能反省呢

例如:

class Person(Base):
    '''
        Represents Person
    '''
    __tablename__ = 'person'

    # Columns
    id = Column(String(8), primary_key=True, default=uid_gen)
    title = Column(String(512), nullable=False)
    birth_date = Column(DateTime, nullable=False)
我想得到这个id,标题,出生日期,确定它们的限制(比如标题是字符串,最大长度是512,或者出生日期是日期时间等等)


谢谢

如果您使用的是
sqlalchemy 0.8
,那么您应该检查一下新功能。从文档中提取的示例代码:

class User(Base):
    __tablename__ = 'user'

    id = Column(Integer, primary_key=True)
    name = Column(String)
    name_syn = synonym(name)
    addresses = relationship(Address)

# universal entry point is inspect()
>>> b = inspect(User)

# column collection
>>> b.columns
[<id column>, <name column>]

很好,谢谢。我没想到有一个元信息不在列中,所以我试着像用户一样访问它
employees = Table(...)
# or if using declarative
#employees = Employee.__table__

# or just
employees.c.employee_id

# via string
employees.c['employee_id']

# iterate through all columns
for c in employees.c:
    print c

# access a column's name, type, nullable, primary key, foreign key
employees.c.employee_id.name
employees.c.employee_id.type
employees.c.employee_id.nullable
employees.c.employee_id.primary_key
employees.c.employee_dept.foreign_keys