Michael Bayer wrote:
you can stick order_by into the ManyToMany, OneToMany, etc....none of
it should be processed until the tables exist in the MetaData and are
therefore locatable via string....
Yah -- this was easy. A quick first cut doing this only (no
generalization yet at all) is attached. I haven't tested it enough
myself to be entirely comfortable yet, and it could probably be made
less ugly -- but early feedback is a Good Thing.
Index: test/ext/activemapper.py
===================================================================
--- test/ext/activemapper.py (revision 1654)
+++ test/ext/activemapper.py (working copy)
@@ -26,7 +26,7 @@
cell_phone = column(String)
work_phone = column(String)
prefs_id = column(Integer, foreign_key=ForeignKey('preferences.id'))
- addresses = one_to_many('Address', colname='person_id', backref='person')
+ addresses = one_to_many('Address', colname='person_id', backref='person', order_by=['state', 'city', 'postal_code'])
preferences = one_to_one('Preferences', colname='pref_id', backref='person')
def __str__(self):
@@ -234,4 +234,4 @@
# go ahead and setup the database connection, and create the tables
# launch the unit tests
- unittest.main()
\ No newline at end of file
+ unittest.main()
Index: lib/sqlalchemy/ext/activemapper.py
===================================================================
--- lib/sqlalchemy/ext/activemapper.py (revision 1654)
+++ lib/sqlalchemy/ext/activemapper.py (working copy)
@@ -47,7 +47,7 @@
#
class relationship(object):
def __init__(self, classname, colname=None, backref=None, private=False,
- lazy=True, uselist=True, secondary=None):
+ lazy=True, uselist=True, secondary=None, order_by=False):
self.classname = classname
self.colname = colname
self.backref = backref
@@ -55,25 +55,28 @@
self.lazy = lazy
self.uselist = uselist
self.secondary = secondary
+ self.order_by = order_by
class one_to_many(relationship):
def __init__(self, classname, colname=None, backref=None, private=False,
- lazy=True):
+ lazy=True, order_by=False):
relationship.__init__(self, classname, colname, backref, private,
- lazy, uselist=True)
+ lazy, uselist=True, order_by=order_by)
class one_to_one(relationship):
def __init__(self, classname, colname=None, backref=None, private=False,
- lazy=True):
+ lazy=True, order_by=False):
if backref is not None:
backref = create_backref(backref, uselist=False)
relationship.__init__(self, classname, colname, backref, private,
- lazy, uselist=False)
+ lazy, uselist=False, order_by=order_by)
class many_to_many(relationship):
- def __init__(self, classname, secondary, backref=None, lazy=True):
+ def __init__(self, classname, secondary, backref=None, lazy=True,
+ order_by=False):
relationship.__init__(self, classname, None, backref, False, lazy,
- uselist=True, secondary=secondary)
+ uselist=True, secondary=secondary,
+ order_by=order_by)
#
@@ -125,12 +128,19 @@
relations = {}
for propname, reldesc in klass.relations.items():
relclass = ActiveMapperMeta.classes[reldesc.classname]
+ if isinstance(reldesc.order_by, str):
+ reldesc.order_by = [ reldesc.order_by ]
+ if isinstance(reldesc.order_by, list):
+ for itemno in range(len(reldesc.order_by)):
+ if isinstance(reldesc.order_by[itemno], str):
+ reldesc.order_by[itemno] = getattr(relclass.c, reldesc.order_by[itemno])
relations[propname] = relation(relclass.mapper,
secondary=reldesc.secondary,
backref=reldesc.backref,
private=reldesc.private,
lazy=reldesc.lazy,
- uselist=reldesc.uselist)
+ uselist=reldesc.uselist,
+ order_by=reldesc.order_by)
class_mapper(klass).add_properties(relations)
if klass in __deferred_classes__:
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users