Peewee加入后获得专栏

时间:2014-09-25 12:32:03

标签: python mysql peewee

我无法读取已加入的另一个表的列。它抛出AttributeError

class Component(Model):
  id = IntegerField(primary_key=True)
  title = CharField()

class GroupComponentMap(Model):
  group = ForeignKeyField(Component, related_name='group_fk')
  service = ForeignKeyField(Component, related_name='service_fk')

现在查询

comp = (Component
        .select(Component, GroupComponent.group.alias('group_id'))
        .join(GroupComponent, on=(Component.id == GroupComponent.group))
       )

for row in comp:
  print row.group_id

现在我收到错误AttributeError: 'Component' object has no attribute 'group_id'

1 个答案:

答案 0 :(得分:8)

如果您只想将group_id属性直接修补到选定的Component,请致电.naive()。这告诉peewee您不想重建连接模型的图形 - 您只需要将所有属性修补到单个Component实例上:

for row in comp.naive():
    print row.group_id  # This will work now.