我在访问代码中的交叉引用(通过)模型时遇到了困难。
从下面的代码中可以看到,我的交叉引用模型中有一个字段,我想通过SoundFile模型来选择,但我似乎不知道如何选择。
我对python和peewee很陌生,所以请容忍我。我有编程经验,只是没有python。我很感谢你能提供的任何帮助。
:,我有一个存储库,在内存中保存着SoundFile记录的列表。我正在遍历这些记录,并试图访问交叉引用模型(用于该模型中的字段)。每个模型扩展的BaseModel只设置类元。
SoundFile模型
from peewee import *
from OgmaChatBot.entities.BaseModel import BaseModel
class SoundFile(BaseModel):
file_name = TextField()
short_name = TextField()
command_available = IntegerField(constraints=[SQL("DEFAULT 0")])
@staticmethod
def get_all():
return SoundFile.select()
@staticmethod
def get_one(key):
return SoundFile.get(SoundFile.id == key)事件模型
from peewee import *
from OgmaChatBot.entities.BaseModel import BaseModel
class Event(BaseModel):
event = TextField()
@staticmethod
def get_all():
return Event.select()
@staticmethod
def get_one(key):
return Event.get(Event.id == key)SoundEvent模型(交叉参考模型)
from peewee import *
from OgmaChatBot.entities.BaseModel import BaseModel
from OgmaChatBot.entities.Event import Event
from OgmaChatBot.entities.SoundFile import SoundFile
class SoundEvent(BaseModel):
sound_file = ForeignKeyField(
column_name='sound_file_id',
field='id',
model=SoundFile,
backref='sound_event'
)
event = ForeignKeyField(
column_name='event_id',
field='id',
model=Event,
backref='sound_event'
)
username = TextField(null=True)
@staticmethod
def get_all():
query = (SoundEvent
.select(SoundEvent, SoundFile, Event)
.join(SoundFile)
.switch(SoundEvent)
.join(Event))
return query
@staticmethod
def get_one(key):
query = (SoundEvent
.select()
.join(SoundFile)
.switch(SoundEvent)
.join(Event)
.where(SoundEvent.id == key))
return query发布于 2018-12-17 02:39:07
您的问题不清楚您想要做什么,下面是一些基于您共享的静态方法的示例:
@staticmethod
def get_all():
query = (SoundEvent
.select(SoundEvent, SoundFile, Event)
.join(SoundFile)
.switch(SoundEvent)
.join(Event))
return query然后可以打印所有文件名:
for sound_event in SoundEvent.get_all():
print(sound_event.sound_file.filename)或打印所有事件:
for sound_event in SoundEvent.get_all():
print(sound_event.event.event)或者,您也可以从SoundFile中选择并列出所有事件:
sound_file = SoundFile.get(SoundFile.filename == 'the-file.mp3')
events = (Event
.select()
.join(SoundEvent)
.where(SoundEvent.sound_file == sound_file))
for event in events:
print(event.event)与两个联接等效:
events = (Event
.select()
.join(SoundEvent)
.join(SoundFile)
.where(SoundFile.filename == 'the-file.mp3'))
for event in events:
print(event.event)我希望这能澄清一些事情。
发布于 2018-12-18 21:58:14
根据执行部分的评论:
我要做的是访问SoundEvent模型中的username字段,从SoundFile模型开始。在我的存储库中,我有一个SoundFile对象的列表。
有两种方法。由于每个声音文件可能有0..n个SoundEvent对象,所以我们将这样做:
for sound_file in sound_files:
events = (SoundEvent
.select(SoundEvent.username)
.where(SoundEvent.sound_file == sound_file))
print(sound_file.filename)
for event in events:
print(' * ', event.username)既然您已经用SoundEvent.sound_file声明了backref="sound_event"外键,您也可以这样做,这是等价的(尽管考虑使用backref sound_events复数):
for sound_file in sound_files:
print(sound_file.filename)
for event in sound_file.sound_event:
print(' * ', event.username)最后一种选择是尝试使用prefetch()进行更有效的操作,这在其实现过程中要复杂一些,并且只应在分析之后使用:
sound_files_with_events = prefetch(sound_files, SoundEvent)
for sound_file in sound_files_with_events:
print(sound_file.filename)
for event in sound_file.sound_event: # the backref is pre-populated!
print(' * ', event.username)https://stackoverflow.com/questions/53773364
复制相似问题