只是 sqlalchemy 过滤器不起作用

发布于 2024-09-12 12:25:57 字数 576 浏览 1 评论 0原文

选择这样的所有作品:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瘫痪情歌 2024-09-19 12:26:01

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 use filter(). Additionaly you can't just use stock_count (probably, you didn't show your table definition code), you have to use products.stock_count or possibly products.__class__.stock_count. So Try:
q=session.query(products).filter(product.stock_count==0)

仅冇旳回忆 2024-09-19 12:26:00

您可能尝试对裸露的 Table 对象使用 orm。

此代码适用于 0.5(基础 centos 6.2 中的版本):

#!/usr/bin/env python
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

db = create_engine(localopts.connect_string)
Session = sessionmaker(bind=db)

Base = declarative_base()
Base.metadata.reflect(bind=db)

class some_table(Base): 
    __table__ = Base.metadata.tables['table_name']

session = Session()

for row in session.query(some_table.username).filter_by(username="some-user"):
    print row

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):

#!/usr/bin/env python
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

db = create_engine(localopts.connect_string)
Session = sessionmaker(bind=db)

Base = declarative_base()
Base.metadata.reflect(bind=db)

class some_table(Base): 
    __table__ = Base.metadata.tables['table_name']

session = Session()

for row in session.query(some_table.username).filter_by(username="some-user"):
    print row
秋心╮凉 2024-09-19 12:26:00

您是否尝试过在 filter_by 之后添加 .all() :

q = session.query(products).filter_by(stock_count=0).all()

have you tried adding a .all() after your filter_by:

q = session.query(products).filter_by(stock_count=0).all()
不必了 2024-09-19 12:26:00

您是否尝试过文字Sql?我有同样的错误消息,但当我使用文字 sql 时,它就消失了。

所以对于你的例子来说它会是这样的:

q = session.query(products).filter('stock_count==0')

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:

q = session.query(products).filter('stock_count==0')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文