0

嗨!如何使用 peewee 选择第二行或第六十行?例如 (1st, 31st, 61st, 91st, ...) 我尝试使用循环,但它非常恶心。每个循环我都会增加偏移量。

我尝试了 peewee.expression 但不幸的是没有用:

TypeError:字符串格式化期间并非所有参数都转换了

data = GreDatasDev3.select().order_by(GreDatasDev3.idGreDatasDev.desc()).where(Expression(GreDatasDev3.idGreDatasDev, '%', 60) == 0).limit(168)
deviceDatasList = []
for i in data:
   deviceDatasList.append(
            {
            "idGreDatasDev" : i.idGreDatasDev,
            "time" : str(i.time),
            "panelNumber" : i.panelNumber,
            "battmV" : i.battmV,
            "battmA" : i.battmA,
            "battW" : i.battW,
            "panemV" : i.panemV,
            "panemA" : i.panemA,
            "paneW" : i.paneW,
            "loadmV" : i.loadmV,
            "loadmA" : i.loadmA,
            "loadW" : i.loadW,
            "battStatus" : i.battStatus,
            "paneStatus" : i.paneStatus,
            "loadStatus" : i.loadStatus,
            "percent" : round(100-((13800-i.battmV)/27), 1),
            "windStatus" : i.windStatus,
            "windMax" : i.windMax,
            "windDirection" : i.windDirection,
            "rainFall" : i.rainFall,
            "humidity" : i.humidity,
            "airPressure" : i.airPressure,
            "temperature" : i.temperature
            }
            )

但没有 .where 标签它正在工作。就在最后 168 行。

data = GreDatasDev3.select().order_by(GreDatasDev3.idGreDatasDev.desc()).limit(168)
        deviceDatasList = []
        for i in data:
            deviceDatasList.append(
            {
            "idGreDatasDev" : i.idGreDatasDev,
            "time" : str(i.time),
            ...
            }
            )
4

1 回答 1

0

您可以使用 ROW_NUMBER 窗口函数。详情在这里

例如:

# Subquery to fetch all users and their rownum (sorted by username)
subq = User.select(
    User,
    fn.ROW_NUMBER().over(order_by=[User.username]).alias('rownum'))

# Get every 6th user:
query = (User
         .select(subq.c.id, subq.c.username)
         .from_(subq)
         .where(Expression(subq.c.rownum, '%', 6) == 0))

for user in query:
    # ...

要使此代码正常工作,您需要:

# Manually import Expression to construct the modulo expression,
# since modulo is not supported out-of-the-box in peewee.
from peewee import Expression
于 2020-07-24T19:37:04.663 回答