保持短小:减少在特定长度后条件语句的中断

2024-10-01 00:25:34 发布

您现在位置:Python中文网/ 问答频道 /正文

这就是我所拥有的:

SomeTable.select.where(reduce(operator.or_, (SomeTable.stuff == entry for entry in big_list)))

当我在big_list中有一个相对较大的元素列表时,问题就出现了:

^{pr2}$

有没有另一种方法不需要把列表分成几个块?在

尝试过使用任何建议,以下是我的错误:

Traceback (most recent call last):
  File "C:/Users/f9xk3li/Documents/GitHub/leoshop_web/leoshop_web/data_models/data_model.py", line 347, in <module>
    search_bins_all("BoA 0")
  File "C:/Users/f9xk3li/Documents/GitHub/leoshop_web/leoshop_web/data_models/data_model.py", line 179, in search_bins_all
    for d in generator.order_by(SomeTable.RetrievedDate.desc()):
  File "C:\Users\f9xk3li\AppData\Local\Continuum\Anaconda\lib\site-packages\peewee.py", line 282, in inner
    clone = self.clone()  # Assumes object implements `clone`.
  File "C:\Users\f9xk3li\AppData\Local\Continuum\Anaconda\lib\site-packages\peewee.py", line 2202, in clone
    return self._clone_attributes(query)
  File "C:\Users\f9xk3li\AppData\Local\Continuum\Anaconda\lib\site-packages\peewee.py", line 2412, in _clone_attributes
    query = super(SelectQuery, self)._clone_attributes(query)
  File "C:\Users\f9xk3li\AppData\Local\Continuum\Anaconda\lib\site-packages\peewee.py", line 2206, in _clone_attributes
    query._where = self._where.clone()
AttributeError: 'bool' object has no attribute 'clone'

这是密码

generator = SomeTable.select()
generator = generator.where(any(SomeTable.BIN == entry for entry in big_list))
for d in generator:
    ....

Tags: inpywebfordataclonelinewhere
2条回答

尝试...where(SomeTable.BIN.in_(big_list))

PeeWee对在其where子句中使用什么来处理库有限制。在

http://docs.peewee-orm.com/en/latest/peewee/querying.html#query-operators

为了扩展Jacob对已批准答案的评论,我想他是说您可以使用子查询而不是解析所有id。在

例如

admin_users = User.select().where(User.is_admin == True)
admin_messages = Message.select().where(Message.user.in_(admin_users))

相关问题 更多 >