peewee 选择在使用模型实例而不是直接使用模型时不起作用的地方 [英] peewee select where not working when using a model instance rather than a model directly
问题描述
我有一个 peewee 模型 my_table = MyTable()
的实例,我想从中选择一些模型实例.
我不明白为什么会这样:
在 [0] [selection.p_name for selection in my_table.select() if selection.p_type == "Solar"]Out [0] ['太阳能,光伏','太阳能,光伏','太阳能,光伏','太阳能,光伏','太阳能,聚光太阳能','太阳能,聚光太阳能']
但这不是:
在 [1] selections = my_table.select().where(my_table.p_type=="Solar")在 [2] [t.p_name for t in selection]出 [2]
什么都没有输出.事实上,len(selections)=0
我做错了什么吗?
我的模型定义在一个文件中,如下所示:
cafe3db = SqliteDatabase(db_fp)类 Cafe3BaseModel(模型):元类:数据库 = cafe3db类场景表(Cafe3BaseModel):path_scenario_key = CharField(primary_key=True)path_type = CharField()路径名称 = CharField()cafe3db.create_tables([ScenarioTable])
然后我填充表格.这是 SQLite 数据库的屏幕截图,如 SQLite 的 DB Browswer 所示:
然后我创建表的一个实例:场景表 = 场景表()
然后,在 Python shell 中,我导入实例:
from x.y import script_table
我知道它拥有我期望的所有模型实例 (112):
<预><代码>>>>len(scenario_table.select())112这有效:
<预><代码>>>>[t.pathway_name for t in scene_table.select() if t.pathway_type == 'Coal']['煤,亚烟','煤,烟','煤,褐煤','煤,亚烟','煤,褐煤','煤,烟','煤,烟','煤,褐煤']但这不是:
<预><代码>>>>[t.pathway_name for t in scene_table.select().where(scenario_table.pathway_type == 'Coal')][]经过反复试验,我能够通过直接导入模型而不是模型的实例来使事情发挥作用.所以,而不是:
from x.y import script_table
我现在有:
from x.y import ScenarioTable
现在:
selections = ScenarioTable.select().where(ScenarioTable.pathway_type=='Coal')[t.pathway_name for t in selection]
确实返回预期的模型实例名称列表.
所以我现在的问题是:为什么模型实例 select
起作用,而模型实例 where
不起作用?
我正在尝试重现您的问题,但我不能.
首先我创建数据库和表:
导入peeweedb = peewee.SqliteDatabase('test.db')数据库连接()类 my_table(peewee.Model):p_name = peewee.CharField()p_type = peewee.CharField()元类:数据库 = dbdb.create_tables([my_table])
然后我插入两行:
d1 = my_table(p_name="太阳能、光伏", p_type="太阳能")d1.save()d2 = my_table(p_name="风车", p_type="风")d2.save()
然后我尝试你的命令:
<预><代码>>>>[t.p_name for t in my_table.select().where(my_table.p_type=="Solar")]['太阳能、光伏']所以它在这里工作 - 也许你做错了什么,但你提供的代码是正确的
I have an instance of a peewee Model my_table = MyTable()
from which I want to select some model instances.
I don't understand why this works:
In [0] [selection.p_name for selection in my_table.select() if selection.p_type == "Solar"]
Out [0] ['Solar, photovoltaic',
'Solar, photovoltaic',
'Solar, photovoltaic',
'Solar, photovoltaic',
'Solar, concentrated solar power',
'Solar, concentrated solar power']
but this doesn't:
In [1] selections = my_table.select().where(my_table.p_type=="Solar")
In [2] [t.p_name for t in selections]
Out [2]
Nothing is output. In fact, len(selections)=0
Am I doing something wrong?
My model definition is in one file and is as follows:
cafe3db = SqliteDatabase(db_fp)
class Cafe3BaseModel(Model):
class Meta:
database = cafe3db
class ScenarioTable(Cafe3BaseModel):
pathway_scenario_key = CharField(primary_key=True)
pathway_type = CharField()
pathway_name = CharField()
cafe3db.create_tables([ScenarioTable])
I then populate the tables. Here is a screen capture of the SQLite database as seen DB Browswer for SQLite:
I then create an instance of the table: scenario_table = ScenarioTable()
Then, in a Python shell, I import the instance:
from x.y import scenario_table
I know it has all the model instances I expect (112):
>>> len(scenario_table.select())
112
And this works:
>>> [t.pathway_name for t in scenario_table.select() if t.pathway_type == 'Coal']
['Coal, sub-bituminous', 'Coal, bituminous', 'Coal, lignite', 'Coal, sub-bituminous', 'Coal, lignite', 'Coal, bituminous', 'Coal, bituminous', 'Coal, lignite']
But this doesn't:
>>> [t.pathway_name for t in scenario_table.select().where(scenario_table.pathway_type == 'Coal')]
[]
After trial and error, I was able to make things work by importing the model directly rather than an instance of the model. So, rather than:
from x.y import scenario_table
I now have:
from x.y import ScenarioTable
and now:
selections = ScenarioTable.select().where(ScenarioTable.pathway_type=='Coal')
[t.pathway_name for t in selections]
Does return the expected list of model instance names.
So my question is now: why does the model instance select
work, but not the model instance where
?
I'm trying to reproduce your problem, but I can't.
First I create the database and the table:
import peewee
db = peewee.SqliteDatabase('test.db')
db.connect()
class my_table(peewee.Model):
p_name = peewee.CharField()
p_type = peewee.CharField()
class Meta:
database = db
db.create_tables([my_table])
Then I insert two rows:
d1 = my_table(p_name="Solar, photovoltaic", p_type="Solar")
d1.save()
d2 = my_table(p_name="Windmill", p_type="Wind")
d2.save()
After that I try your command:
>>> [t.p_name for t in my_table.select().where(my_table.p_type=="Solar")]
['Solar, photovoltaic']
So it works here - maybe you're doing something else wrong, but the code you provided is correct
这篇关于peewee 选择在使用模型实例而不是直接使用模型时不起作用的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!