2014-09-25 80 views
5

我無法讀取已加入的另一個表的列。它拋出AttributeError的Peewee在加入後獲取列

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'

+0

行有ATTRS'id'和'title',你是什麼意思是由'group_id'? – hyades 2014-09-25 12:38:08

+1

在'select'我已經添加了'GroupComponent.group.alias('group_id')''所以我想要得到'group_id'的值 – Rishabh 2014-09-25 14:00:31

回答

6

如果你只是想直接修補group_id屬性到所選Component,叫.naive()。這會指示你不想重建連接模型的圖形peewee - 你只是想所有屬性貼在一個組件實例:

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