[sqlalchemy] Change relationship order_by on runtime

2011-10-25 Thread neurino
I need to change a collection order_by when a parent instance mapped
attribute changes.

Something like this:

parents = Table('parents', metadata,
Column('id', Integer, primary_key=True)
Column('reverse', Boolean, primary_key=True)
)
items = Table('items', metadata,
Column('id', Integer, primary_key=True)
)

mapper(parents, Parent,
properties={
'items': relationship(Item,
backref='parent',
collection_class=ItemsList,
order_by=items.c.id
)
}

 parent = session.query(Parent).first()
 parent.reverse
False
 parent.items
[Item 1, Item 2, Item 3]
 parent.reverse = True
 parent.items
[Item 3, Item 2, Item 1]

I already use a custom collection and I could make custom __iter__
according to first item (if any) `self.parent.reverse` but it seems to
me a bit too tricky.

Any advice?

Thank you for your support

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@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.



Re: [sqlalchemy] Change relationship order_by on runtime

2011-10-25 Thread Michael Bayer
you can use a dynamic relationship which allows you to say:

some_parent.items.order_by(items.c.id.desc())

http://www.sqlalchemy.org/docs/orm/collections.html#dynamic-relationship-loaders


On Oct 25, 2011, at 5:02 AM, neurino wrote:

 I need to change a collection order_by when a parent instance mapped
 attribute changes.
 
 Something like this:
 
parents = Table('parents', metadata,
Column('id', Integer, primary_key=True)
Column('reverse', Boolean, primary_key=True)
)
items = Table('items', metadata,
Column('id', Integer, primary_key=True)
)

mapper(parents, Parent,
properties={
'items': relationship(Item,
backref='parent',
collection_class=ItemsList,
order_by=items.c.id
)
}
 
 parent = session.query(Parent).first()
 parent.reverse
False
 parent.items
[Item 1, Item 2, Item 3]
 parent.reverse = True
 parent.items
[Item 3, Item 2, Item 1]
 
 I already use a custom collection and I could make custom __iter__
 according to first item (if any) `self.parent.reverse` but it seems to
 me a bit too tricky.
 
 Any advice?
 
 Thank you for your support
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To post to this group, send email to sqlalchemy@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.
 

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@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.



Re: [sqlalchemy] Change relationship order_by on runtime

2011-10-25 Thread neurino
Thank you very much

On Tue, Oct 25, 2011 at 3:46 PM, Michael Bayer mike...@zzzcomputing.comwrote:

 you can use a dynamic relationship which allows you to say:

 some_parent.items.order_by(items.c.id.desc())


 http://www.sqlalchemy.org/docs/orm/collections.html#dynamic-relationship-loaders


 On Oct 25, 2011, at 5:02 AM, neurino wrote:

  I need to change a collection order_by when a parent instance mapped
  attribute changes.
 
  Something like this:
 
 parents = Table('parents', metadata,
 Column('id', Integer, primary_key=True)
 Column('reverse', Boolean, primary_key=True)
 )
 items = Table('items', metadata,
 Column('id', Integer, primary_key=True)
 )
 
 mapper(parents, Parent,
 properties={
 'items': relationship(Item,
 backref='parent',
 collection_class=ItemsList,
 order_by=items.c.id
 )
 }
 
  parent = session.query(Parent).first()
  parent.reverse
 False
  parent.items
 [Item 1, Item 2, Item 3]
  parent.reverse = True
  parent.items
 [Item 3, Item 2, Item 1]
 
  I already use a custom collection and I could make custom __iter__
  according to first item (if any) `self.parent.reverse` but it seems to
  me a bit too tricky.
 
  Any advice?
 
  Thank you for your support
 
  --
  You received this message because you are subscribed to the Google Groups
 sqlalchemy group.
  To post to this group, send email to sqlalchemy@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.
 

 --
 You received this message because you are subscribed to the Google Groups
 sqlalchemy group.
 To post to this group, send email to sqlalchemy@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.



-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@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.