I got it to work. Here's what I did: distance_function = ( 3959 * func.acos(func.cos(func.radians(*bindparam*('origin_lat')))) * func.cos(func.radians(places_table.c.latitude)) * func.cos(func.radians(places_table.c.longitude) - func.radians(* bindparam*('origin_lng'))) + func.sin(func.radians(*bindparam*('origin_lat'))) * func.sin(func.radians(places_table.c.latitude)) )
mapper(Place, places_table, properties={ 'id':places_table.c.id, 'longitude' : places_table.c.longitude, 'latitude' : places_table.c.latitude, 'distance' : column_property(distance_function) } ) (Places are joined with things, as before) And then: if sort_by == 'alphabetical': order_by = asc(Thing.title) bind_params = {} elif sort_by == "location": order_by = asc(Place.distance) bind_params = {'origin_lat':37, 'origin_lng':-122} else: order_by = desc(Thing.creation) bind_params = {} things = session.query(Thing).\ options(eagerloads).\ join(Look.place).\ ... params(bind_params).\ all() SQLAlchemy is cooler every time I use it! Thanks to everyone for their help! -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/lKQrx809z8IJ. 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.