Sqlalchemy 打印查询的输出对象
Sqlalchemy print the output object of a query
我一直在寻找,但找不到任何东西...
我的模型是,
class Departament(db.Model):
id = Column(Integer, primary_key=True)
departament = Column(String, unique=True)
depmv = relationship('Mvpordep', backref='line', lazy='select')
depfac = relationship('Bill', backref='line_fac', lazy='select')
def __init__(self, departament):
self.departament = departament
def __repr__(self):
return '{}'.format(self.departament)
然后你试试
departament = Departament.query.filter_by(departament='ti').first()
print(departament.id)
但它给了我以下错误
AttributeError: 'NoneType' object has no attribute 'id'
当我获得多行并使用 for 循环对其进行迭代时,没问题。但是如果我尝试用 for 循环来做,合乎逻辑的事情就会发生
for out in departament:
print(out.id)
TypeError: 'NoneType' object is not iterable
我不明白发生了什么
您正在 class 部门使用 __repr__()
功能。因此,如果您从部门 class 获取数据,您将只会获得部门的数据(如您创建的那样)。
这里的错误是,你获取的数据中没有id,它只是部门。这就是为什么你得到 Nonetype
错误。
如果您想要部门 class 中的所有数据,请从部门 class 中删除 __repr__()
函数(和 __init__()
)或声明 [=10] 中的每个列名=] 函数(和 __init__()
)。
我一直在寻找,但找不到任何东西...
我的模型是,
class Departament(db.Model):
id = Column(Integer, primary_key=True)
departament = Column(String, unique=True)
depmv = relationship('Mvpordep', backref='line', lazy='select')
depfac = relationship('Bill', backref='line_fac', lazy='select')
def __init__(self, departament):
self.departament = departament
def __repr__(self):
return '{}'.format(self.departament)
然后你试试
departament = Departament.query.filter_by(departament='ti').first()
print(departament.id)
但它给了我以下错误
AttributeError: 'NoneType' object has no attribute 'id'
当我获得多行并使用 for 循环对其进行迭代时,没问题。但是如果我尝试用 for 循环来做,合乎逻辑的事情就会发生
for out in departament:
print(out.id)
TypeError: 'NoneType' object is not iterable
我不明白发生了什么
您正在 class 部门使用 __repr__()
功能。因此,如果您从部门 class 获取数据,您将只会获得部门的数据(如您创建的那样)。
这里的错误是,你获取的数据中没有id,它只是部门。这就是为什么你得到 Nonetype
错误。
如果您想要部门 class 中的所有数据,请从部门 class 中删除 __repr__()
函数(和 __init__()
)或声明 [=10] 中的每个列名=] 函数(和 __init__()
)。