python-AttributeError:“ NoneType”对象在Flask,SQLAlchemy中没有属性“ time_recorded”
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-AttributeError:“ NoneType”对象在Flask,SQLAlchemy中没有属性“ time_recorded”,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1233字,纯文字阅读大概需要2分钟。
内容图文
![python-AttributeError:“ NoneType”对象在Flask,SQLAlchemy中没有属性“ time_recorded”](/upload/InfoBanner/zyjiaocheng/887/f1af38337e8547ab8f815b182c400a77.jpg)
我有一个api端点,该端点传递一个变量,该变量用于在数据库中进行调用.由于某种原因,它无法运行查询,但语法正确.我的代码如下.
@app.route('/api/update/<lastqnid>')
def check_new_entries(lastqnid):
result = Trades.query.filter_by(id=lastqnid).first()
new_entries = Trades.query.filter(Trades.time_recorded > result.time_recorded).all()
id字段是:
id = db.Column(db.String,default=lambda: str(uuid4().hex), primary_key=True)
我尝试使用filter而不是filter_by,但它不起作用.当我删除filter_by(id = lastqnid)时,它可以工作.不运行查询的原因可能是什么?
正在查询的交易表是
class Trades(db.Model):
id = db.Column(db.String,default=lambda: str(uuid4().hex), primary_key=True)
amount = db.Column(db.Integer, unique=False)
time_recorded = db.Column(db.DateTime, unique=False)
解决方法:
您似乎遇到的问题是在使用结果前不检查是否找到了任何东西
@app.route('/api/update/<lastqnid>')
def check_new_entries(lastqnid):
result = Trades.query.filter_by(id=lastqnid).first()
# Here result may very well be None, so we can make an escape here
if result == None:
# You may not want to do exactly this, but this is an example
print("No Trades found with id=%s" % lastqnid)
return redirect(request.referrer)
new_entries = Trades.query.filter(Trades.time_recorded > result.time_recorded).all()
内容总结
以上是互联网集市为您收集整理的python-AttributeError:“ NoneType”对象在Flask,SQLAlchemy中没有属性“ time_recorded”全部内容,希望文章能够帮你解决python-AttributeError:“ NoneType”对象在Flask,SQLAlchemy中没有属性“ time_recorded”所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。