Peewee gets a column after joining

I cannot read the column of another table that is joined. It throws an 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') 

Now request

 comp = (Component .select(Component, GroupComponent.group.alias('group_id')) .join(GroupComponent, on=(Component.id == GroupComponent.group)) ) for row in comp: print row.group_id 

Now I get the AttributeError: 'Component' object has no attribute 'group_id' error message AttributeError: 'Component' object has no attribute 'group_id'

+5
source share
1 answer

If you just want to directly fix the group_id attribute on the selected Component , call .naive() . This makes it clear that you do not want to restore the graph of the combined models - you just want all the attributes to be fixed on one instance of Component:

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

Source: https://habr.com/ru/post/1203397/


All Articles