【发布时间】:2021-10-15 03:30:05
【问题描述】:
我一直在寻找,但找不到任何东西......
我的模型是,
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
我不明白会发生什么
【问题讨论】:
-
将此行从 'Departament.query.filter_by(departament='ti').first()' 更改为 'Departament.objects.filter_by(departament='ti').first()'
-
运气不好
AttributeError: type object 'Departament' has no attribute 'objects' -
您的实际型号名称是Departmento 不仅是Department 最后还有一个“o”
-
是的,你是对的,对不起。写帖子的时候出错了。非常感谢您的提示。最后我已经找到了问题所在。这是因为开发数据库和生产数据库不同,并且开发中不存在数据。非常感谢您的帮助!
标签: python flask sqlalchemy orm flask-sqlalchemy