只是 sqlalchemy 过滤器不起作用
选择这样的所有作品:
q = session.query(products)
现在我想添加一个 WHERE 过滤器,所以我正在尝试:
q = session.query(products).filter_by(stock_count=0)
我收到一条错误消息,说“nonetype”对象没有属性“class_manager”。
不确定问题是什么?
更新 该列似乎映射得很好,就像我所做的那样:
q = session.query(products)
for p in q:
print p.stock_count
它输出值。
但如果我这样做:
p.stock_count = 6
我也会收到错误,说:“无法设置属性”
所以我可以查询它,但将列添加为过滤器,或设置值会导致错误。
奇怪不是吗?
Select all works like this:
q = session.query(products)
Now I want to add a WHERE filter, so I am trying:
q = session.query(products).filter_by(stock_count=0)
I get an error saying 'nonetype' object has no attribute 'class_manager'.
Not sure what the issue is?
Update
The column seems to be mapped fine, as when I do:
q = session.query(products)
for p in q:
print p.stock_count
It outputs the value.
But if I do:
p.stock_count = 6
I get an error also, saying: "can't set attribute"
So I can query for it, but adding the column as a filter, OR setting the value causes an error.
Strange no?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
filter_by()
使用关键字字典,您实际上想要使用filter()
。另外,您不能只使用stock_count
(可能您没有显示表定义代码),您必须使用products.stock_count
或可能products。 __class__.stock_count
。所以尝试:q=session.query(products).filter(product.stock_count==0)
filter_by()
works with a keyword dictionary, you actually want to usefilter()
. Additionaly you can't just usestock_count
(probably, you didn't show your table definition code), you have to useproducts.stock_count
or possiblyproducts.__class__.stock_count
. So Try:q=session.query(products).filter(product.stock_count==0)
您可能尝试对裸露的 Table 对象使用 orm。
此代码适用于 0.5(基础 centos 6.2 中的版本):
You may be trying to use the orm against a bare Table object.
This code works on 0.5 (the one in base centos 6.2):
您是否尝试过在 filter_by 之后添加 .all() :
have you tried adding a .all() after your filter_by:
您是否尝试过文字Sql?我有同样的错误消息,但当我使用文字 sql 时,它就消失了。
所以对于你的例子来说它会是这样的:
Have you tried Literal Sql? I've had the same error message but when I used literal sql it was gone.
So for your example it would be something like: