如何在 peewee 中进行此 SQL 查询
How to make this SQL query in peewee
我正在将我的代码从 sqlite3
移植到 peewee
,到目前为止效果很好,但是我不知道如何移植这个查询:
query = ("SELECT * FROM things
WHERE Fecha substr(Fecha,7)||'/'||substr(Fecha,4,2)||'/'||substr(Fecha,1,2)
BETWEEN ? AND ?", my_field, my_second_field)
在 peewee 上,我已经走到这一步了:
where_condition = database.fecha.fn.Substr(database.fecha, 7)
.concat('/')
.concat(database.fecha.fn.Substr(database.fecha, 4,2))
.concat('/')
.concat(database.fecha.fn.Substr(database.fecha, 1,2))
.between(my_field, my_second_field)
database.select().where(where_condition)
但它不起作用,我对如何在 peewee 上连接子字符串一无所知。
>>> 'TextField' has no attribute 'fn'
更新:
where_condition 是一个外部变量非常重要,因为它只是众多过滤器中的一个。我用不同的方法制作不同的过滤器,然后将它们作为元组一起传递给 where
(我认为 fn
函数因此不起作用)。完整的代码是:
for record in database.select().where(*self.build_where_conditions())
# Do something
def build_where_conditions(self):
where_condition = []
# first_thing
if self.checkfirst_thing.isChecked():
where_condition.append(database.first_thing == first_thing)
# something
if self.checksomething.isChecked():
where_condition.append(database.something == something)
# client
if self.checkclient.isChecked():
where_condition.append(database.client == client)
# Serie
if self.checkSerie.isChecked():
where_condition.append(database.code.startswith("{0}{1}".format("18", serie)))
# Code
if self.checkCode.isChecked():
where_condition.append(database.code.between(
"{0}{1}".format("18", code1),
"{0}{1}".format("18", code2)))
# NOTE: Must always return a tuple
if len(where_condition) >= 1:
return where_condition
return (None,)
如有任何关于如何正确执行此操作的建议,我们将不胜感激。
"fn"应该是独立的。它不是一个属性(就像错误说的那样......呃)。
from peewee import fn
where_condition = fn.Substr(TheModel.the_text_field, 7)
.concat('/')
.concat(fn.Substr(TheModel.the_text_field, 4,2))
.concat('/') ... etc
我正在将我的代码从 sqlite3
移植到 peewee
,到目前为止效果很好,但是我不知道如何移植这个查询:
query = ("SELECT * FROM things
WHERE Fecha substr(Fecha,7)||'/'||substr(Fecha,4,2)||'/'||substr(Fecha,1,2)
BETWEEN ? AND ?", my_field, my_second_field)
在 peewee 上,我已经走到这一步了:
where_condition = database.fecha.fn.Substr(database.fecha, 7)
.concat('/')
.concat(database.fecha.fn.Substr(database.fecha, 4,2))
.concat('/')
.concat(database.fecha.fn.Substr(database.fecha, 1,2))
.between(my_field, my_second_field)
database.select().where(where_condition)
但它不起作用,我对如何在 peewee 上连接子字符串一无所知。
>>> 'TextField' has no attribute 'fn'
更新:
where_condition 是一个外部变量非常重要,因为它只是众多过滤器中的一个。我用不同的方法制作不同的过滤器,然后将它们作为元组一起传递给 where
(我认为 fn
函数因此不起作用)。完整的代码是:
for record in database.select().where(*self.build_where_conditions())
# Do something
def build_where_conditions(self):
where_condition = []
# first_thing
if self.checkfirst_thing.isChecked():
where_condition.append(database.first_thing == first_thing)
# something
if self.checksomething.isChecked():
where_condition.append(database.something == something)
# client
if self.checkclient.isChecked():
where_condition.append(database.client == client)
# Serie
if self.checkSerie.isChecked():
where_condition.append(database.code.startswith("{0}{1}".format("18", serie)))
# Code
if self.checkCode.isChecked():
where_condition.append(database.code.between(
"{0}{1}".format("18", code1),
"{0}{1}".format("18", code2)))
# NOTE: Must always return a tuple
if len(where_condition) >= 1:
return where_condition
return (None,)
如有任何关于如何正确执行此操作的建议,我们将不胜感激。
"fn"应该是独立的。它不是一个属性(就像错误说的那样......呃)。
from peewee import fn
where_condition = fn.Substr(TheModel.the_text_field, 7)
.concat('/')
.concat(fn.Substr(TheModel.the_text_field, 4,2))
.concat('/') ... etc