Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Peewee getting column after join

I am unable to read the column of another table which is joined. It throws 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 the query is

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 an error AttributeError: 'Component' object has no attribute 'group_id'

like image 340
Rishabh Avatar asked Sep 25 '14 12:09

Rishabh


1 Answers

If you just want to directly patch the group_id attribute onto the selected Component, call .naive(). This instructs peewee that you don't want to reconstruct the graph of joined models -- you just want all attributes patched onto a single Component instance:

for row in comp.naive():
    print row.group_id  # This will work now.
like image 86
coleifer Avatar answered Sep 29 '22 13:09

coleifer