我有以下模型类:
class Ticket(BaseModel):
event = ForeignKeyField(Event)
category = ForeignKeyField(Category)
order_number = IntegerField()
tier_name = CharField()
num_available = IntegerField()
price = DecimalField()
如您所见,该表有多个外键。然后我希望能够选择所有票并对其进行迭代并访问类别和事件:
tickets = Ticket.select()
for ticket in tickets:
print ticket.category.id
print ticket.event.id
但是,我不希望在每次迭代期间针对类别和事件启动新查询。我知道,如果我只是访问该事件,我可以执行以下操作:
tickets = Ticket.select(Ticket, Event).join(Event)
但是,每次访问该类别时仍会启动一个新查询。这段代码给了我一个例外:
tickets = Ticket.select(Ticket, Category, Event).join(Event).join(Category)
我得到的错误是:ValueError:类'models.ticketing.Category'和类'models.event.Event'之间没有外键