简体   繁体   English

如何在联接查询中使用peewee从`ForeignKeyField`获取值?

[英]How to get values from `ForeignKeyField` using peewee in join query?

I have models like this ( peewee 2.10 ): 我有这样的模型( peewee 2.10 ):

class LegalAct(Model):
    start_date = DateField()
    expiration_date = DateField()

class SomeModel(Model):
    name = CharField()
    legal_act = ForeignKeyField(LegalAct)

class OtherModel(Model):
    name = Charfield()
    some_model = ForeignKeyField(SomeModel) 
    starting_legal_act = ForeignKeyField(LegalAct)
    closing_legal_act = ForeignKeyField(LegalAct)

class ExtraModel(Model):
    value = IntegerField()
    other_model = ForeignKeyField(OtherModel) 
    starting_legal_act = ForeignKeyField(LegalAct)
    closing_legal_act = ForeignKeyField(LegalAct)

Given list of SomeModle.id s and a date (to filter LegalAct ) I want to get following data: 给定SomeModle.id的列表和date (以过滤LegalAct ),我想获取以下数据:

SomeModel.name
OtherModel.name
OtherModel.starting_legal_act.start_date
ExtraModel.starting_legal_act.start_date
ExtraModel.value

The issue is that I'm can't figure out how to traverse to OtherModel.starting_legal_act.start_date and ExtraModel.starting_legal_act.start_date - I only able to get id of the corresponding models and fetch the data in consequence queries. 问题是我不知道如何遍历OtherModel.starting_legal_act.start_dateExtraModel.starting_legal_act.start_date我只能获取相应模型的id并在结果查询中获取数据。

My current code is like this: 我当前的代码是这样的:

other_model_legal_act = get_other_legal_act(date)  # a query
extra_model_legal_act = get_extra_legal_act(date)  # a query

data = OtherModel.select(
    SomeModel.name
    OtherModel.name
    OtherModel.starting_legal_act.alias('date_1')  # I get `id`, but want date
    ExtraModel.starting_legal_act.alias('date_2')  # I get `id`, but want date
    ExtraModel.value
).join(SomeModel).switch(OtherModel).join(ExtraModel).where(
    (OtherModel.legal_act == other_model_legal_act) &
    (ExtraModel.legal_act == extra_model_legal_act) &
    (SomeModel.id.in_(id_list))

)

I need to replace these lines with code that will return actual dates instead of record ids: 我需要将这些行替换为将返回实际日期而不是记录ID的代码:

OtherModel.starting_legal_act.alias('date_1')  # I get `id`, but want date
ExtraModel.starting_legal_act.alias('date_2')  # I get `id`, but want date

You want model aliases to reference the LegalAct multiple times: 您希望模型别名多次引用LegalAct:

LegalAct1 = LegalAct.alias()
LegalAct2 = LegalAct.alias()

data = OtherModel.select(
    SomeModel.name
    OtherModel.name
    LegalAct1.start_date.alias('date_1'),
    LegalAct2.start_date.alias('date_2'),
    OtherModel.starting_legal_act.alias('date_1')  # I get `id`, but want date
    ExtraModel.starting_legal_act.alias('date_2')  # I get `id`, but want date
    ExtraModel.value
)
.join(LegalAct1, on=(OtherModel.starting_legal_act == LegalAct.id))
.switch(OtherModel)
# 

etc. 等等

In the future...please try to make your models easier to reason about. 将来...请尝试使您的模型更容易推理。 "SomeModel" and "OtherModel" are absolutely useless. “ SomeModel”和“ OtherModel”绝对没有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM