RE: [sqlalchemy] Re: To select only some columns from some tables using session object, relation many-to-many
Alvaro Reinoso wrote: It works out, thank you! How could I just retrieve some columns from both tables? For example, if I try to select some columns from Item and Channel, I get class 'sqlalchemy.util.NamedTuple' when I'd like to get a channel type with its items: result = session.query(Channel.title, Item.title).join('items').filter(Item.typeItem == zeppelin/ channel).order_by(Channel.titleView).all() I just need some values many times, I don't need to retrieve the whole object. Thanks in advance! It sounds like you are looking for deferred column loading: http://www.sqlalchemy.org/docs/mappers.html#deferred-column-loading You can mark certain columns as not to be loaded until they are accessed. This can be done at mapper definition time as well as at query time. Hope that helps, Simon -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
[sqlalchemy] Re: To select only some columns from some tables using session object, relation many-to-many
It works out, thank you! How could I just retrieve some columns from both tables? For example, if I try to select some columns from Item and Channel, I get class 'sqlalchemy.util.NamedTuple' when I'd like to get a channel type with its items: result = session.query(Channel.title, Item.title).join('items').filter(Item.typeItem == zeppelin/ channel).order_by(Channel.titleView).all() I just need some values many times, I don't need to retrieve the whole object. Thanks in advance! On Aug 3, 1:40 am, Kalium raymond.ma...@gmail.com wrote: On Aug 3, 8:43 am, Alvaro Reinoso alvrein...@gmail.com wrote: Hello, I have these classes where items (class Item) is related to channel object. Channel can contain many items: channel_items = Table( channel_items, metadata, Column(channel_id, Integer, ForeignKey(channels.id)), Column(item_id, Integer, ForeignKey(Item.id)) ) class Channel(rdb.Model): rdb.metadata(metadata) rdb.tablename(channels) id = Column(id, Integer, primary_key=True) title = Column(title, String(100)) items = relation(Item, secondary=channel_items, backref=channels) class Item(rdb.Model): rdb.metadata(metadata) rdb.tablename(items) id = Column(id, Integer, primary_key=True) title = Column(title, String(100)) I know how to get all the columns using something like: session = rdb.Session() channels = session.query(Channel).order_by(Channel.title) However, I'd like to select some columns from both tables with some conditions in Item. For example, select all the channels where item.type = 'jpg'. I'd like to get a channel object with items attributes with that condition for example. How can I do that? I've tried something like (no one worked out): result = session.query(Channel).filter(Item.typeItem != 'zeppelin/ channel').all() result = session.query(Channel, Item).filter(Item.typeItem != 'zeppelin/channel').all() Thanks in advance! Try something like session.query(Channel).join('items').filter(Item.typeItem != 'whatever').all() -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
[sqlalchemy] Re: To select only some columns from some tables using session object, relation many-to-many
On Aug 3, 8:43 am, Alvaro Reinoso alvrein...@gmail.com wrote: Hello, I have these classes where items (class Item) is related to channel object. Channel can contain many items: channel_items = Table( channel_items, metadata, Column(channel_id, Integer, ForeignKey(channels.id)), Column(item_id, Integer, ForeignKey(Item.id)) ) class Channel(rdb.Model): rdb.metadata(metadata) rdb.tablename(channels) id = Column(id, Integer, primary_key=True) title = Column(title, String(100)) items = relation(Item, secondary=channel_items, backref=channels) class Item(rdb.Model): rdb.metadata(metadata) rdb.tablename(items) id = Column(id, Integer, primary_key=True) title = Column(title, String(100)) I know how to get all the columns using something like: session = rdb.Session() channels = session.query(Channel).order_by(Channel.title) However, I'd like to select some columns from both tables with some conditions in Item. For example, select all the channels where item.type = 'jpg'. I'd like to get a channel object with items attributes with that condition for example. How can I do that? I've tried something like (no one worked out): result = session.query(Channel).filter(Item.typeItem != 'zeppelin/ channel').all() result = session.query(Channel, Item).filter(Item.typeItem != 'zeppelin/channel').all() Thanks in advance! Try something like session.query(Channel).join('items').filter(Item.typeItem != 'whatever').all() -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.