如果我有下表:

class Ticket(BaseModel):
    event = ForeignKeyField(Event)
    category = ForeignKeyField(TicketCategory)
    order_number = IntegerField()
    tier_name = CharField()
    num_available = IntegerField()
   price = DecimalField()

然后我执行以下代码:
tickets = Ticket.select()
for ticket in tickets:

     print ticket.event.id

访问外部对象的主键是否强制peewee启动另一个查询?或者peewee聪明到知道身份证已经可用了?

最佳答案

它执行另一个查询。为了避免这种情况:

Ticket.select(Ticket, Event).join(Event)

http://peewee.readthedocs.org/en/latest/peewee/querying.html#saving-queries-by-selecting-related-models

关于python - Python Peewee外键,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22017699/

10-16 05:21