获取交叉参考模型

时间:2018-12-14 04:01:38

标签: python peewee

我在访问代码中的交叉引用(通过)模型时遇到麻烦。

从下面的代码中可以看到,我的交叉引用模型中有一个字段,我想通过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

2 个答案:

答案 0 :(得分:-1)

您的问题不清楚您要做什么...但是下面是一些基于您共享的静态方法的示例:

@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)

我希望这可以弄清楚。

答案 1 :(得分:-1)

基于OP的评论:

  

我想做的是从SoundFile模型开始访问SoundEvent模型中的用户名字段。在我的存储库中,我有一个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)

由于已经用backref="sound_event"声明了SoundEvent.sound_file外键,所以您也可以这样做,这是等效的(尽管考虑将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)