【问题标题】:Building a FIQL query with peewee使用 peewee 构建 FIQL 查询
【发布时间】:2023-04-03 20:31:01
【问题描述】:

我正在尝试创建一个函数,该函数将采用 FIQL 格式化字符串并返回 peewee 表达式。

假设我有 FIQL 格式的 url 参数,如下所示:

fiql_str = "name==Foo,(country==USA;city==Kansas)"

使用FIQL Parser 我可以取回这个对象:

['OR', ('name', '==', 'Foo'), ['AND', ('country', '==', 'USA'), ('city', '==', 'Kansas')]]

我想做的是创建一个接受上述对象的函数,并创建一个 peewee 可以理解的表达式。我习惯了django & Q objects,我可以像这样将表达式链接在一起:

fm = Q()
for mapping in mappings:
    fm |= Q(subscription__approver=mapping.user)
return self.filter(fm)

我试过用 peewee 的 Query Builder / Node 来模仿这个:

def map_to_filter(expressions, node):
    expression = expressions.pop(0)
    if type(expression) == str:
        if expression == "OR":
            node |= map_to_filter(expressions, node)
        if expression == "AND":
            node &= map_to_filter(expressions, node)
    elif type(expression) == tuple:
        return getattr(Store, expression[0]) + expression[1] + expression[2]
    elif type(expression) == list:
        map_to_filter(expression, node)
    return node

result = map_to_filter(expressions, peewee.Node())

但我收到 NotImplementedError:

/lib/python3.7/site-packages/peewee.py in __sql__(self, ctx)
    616
    617     def __sql__(self, ctx):
--> 618         raise NotImplementedError
    619
    620     @staticmethod

NotImplementedError:

是否可以构建这样的功能?否则,还有哪些其他工具/插件可用于解决此问题?

【问题讨论】:

    标签: python peewee flask-peewee


    【解决方案1】:

    您的问题来自于使用裸 Node,它不对应于任何 SQL(因此,没有 sql 方法)。

    我建议累积一个列表并使用functools.reduce() 组合它们。

    例如,

    list_of_conds = [
        (model.field1 == 'val1'),
        (model.field2 == 'bar')]
    reduce(operator.and_, list_of_conds)
    

    您可以在必要时将 reduce 函数切换为 operator.or_ 并继续使用深度优先搜索。

    【讨论】:

      猜你喜欢
      • 2015-03-03
      • 2021-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-25
      • 1970-01-01
      相关资源
      最近更新 更多