Hi Richard, my_user is an instance of db.auth_user, e.g. my_user = db.auth_user[1]
or my_user = db(db.auth_user).select().first() sorry my terminology is a bit rubbish, the crux of my problem is how to order records in a particular table according to the number of records in a different table which reference them. e.g. given a table MusicListener(fields: id, name) and another table called Album(fields:id, name, listener) where listener is a foreign key to the MusicListener table. In web2py the following code will give me the number of albums which a particular music listener has: listener = db.MusicListener[0] no_albums = listener.Album.count() I want to be able to order the records in MusicListener by the number of albums they have: db(db.MusicListener).select(orderby=db.MusicListener.Album.count()) so that it calculates the count for each MusicListener and orders them accordingly. I understand why this code doesn't work but I would prefer my workaround to be elegant and so I posted here in order to try and take advantage of other people's experience! Cheers, John On Wednesday, March 7, 2012 3:57:07 PM UTC+1, Richard wrote: > > Not understand what is my_user... > > Did you affect db to my_user?? > > my_user = db? > > Richard > > > Hi, >> >> I have a table - called table - which keys into the auth_user table. This >> means that I can execute the following code: >> >> my_user.table.count(), my_user.table.select() where my_user is a record >> of type db.auth_user. This is a really nice feature, I'm wondering if there >> is some way to use this to order results... >> >> Normally, when specifying an orderby I might use something like >> orderby=db.auth_user.join_date (where join date is a field on the user >> table). >> >> However this retrieval of a SET of tables which reference a given user >> only works with an instance of auth_user. db.auth_user.table throws an >> error as table is not a Field of this model.. >> >> So, what I want to do is - orderby=db.auth_user.table.count() - to get a >> list of users ordered by how many tables they have. >> >> Sadly, this doesn't work. Is there a way implement what I want to do? >> >> Cheers, >> >> John >> > > On Wednesday, March 7, 2012 3:57:07 PM UTC+1, Richard wrote: > > Not understand what is my_user... > > Did you affect db to my_user?? > > my_user = db? > > Richard > > Hi, >> >> I have a table - called table - which keys into the auth_user table. This >> means that I can execute the following code: >> >> my_user.table.count(), my_user.table.select() where my_user is a record >> of type db.auth_user. This is a really nice feature, I'm wondering if there >> is some way to use this to order results... >> >> Normally, when specifying an orderby I might use something like >> orderby=db.auth_user.join_date (where join date is a field on the user >> table). >> >> However this retrieval of a SET of tables which reference a given user >> only works with an instance of auth_user. db.auth_user.table throws an >> error as table is not a Field of this model.. >> >> So, what I want to do is - orderby=db.auth_user.table.count() - to get a >> list of users ordered by how many tables they have. >> >> Sadly, this doesn't work. Is there a way implement what I want to do? >> >> Cheers, >> >> John >> > >