gpt4 book ai didi

python - 使用 peewee 合并具有不同名称的字段

转载 作者:太空狗 更新时间:2023-10-29 21:06:51 25 4
gpt4 key购买 nike

我正在使用 peewee作为 ORM 并有两个这样的类:

class A(Model):
name = CharField()
body = TextField()

class B(Model):
title = CharField()
body = TextField()

我想从 AB 中获取所有条目,它们的 title/name 以一些字符开头像 'abc'。根据文档,| 运算符应该有所帮助,但我什至无法执行生成的 Expression。显然,我希望在幕后有一个 UNIONAS 表达式。我如何通过 peewee 获得这个?

最佳答案

你应该能够通过类似的东西获得你想要的结果

result = (
A().select(A.name.alias('name_title'), A.body).where(A.name == 'abc') |
B().select(B.title.alias('name_title'), B.body).where(B.title == 'abc')
).select().execute()

search_text = 'abc'
table_a_results = A().select(
A.name.alias('name_title'),
A.body
).where(A.name == search_text)
table_b_results = B().select(
B.name.alias('name_title'),
B.body
).where(B.title == search_text)

result = ( table_a_results | table_b_results ).select().execute()

.alias() 方法根据 docs 为您提供 AS 功能

关于python - 使用 peewee 合并具有不同名称的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37400162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com