Correction: res = {} for hpoint in session.query(HistoryPoint).all(): res.setdefault(hpoint.vehicle_id, []).append({'id':hpoint.id,'location': str(hpoint.location)}) But it will yield a dict, not an array of single-value dicts as requested (are you sure this is what you want? anyways, it's trivial to change).
On Tuesday, September 22, 2015 at 8:33:13 PM UTC+3, Yegor Roganov wrote: > > Just do it in Python: > res = [] > for hpoint in session.query(HistoryPoint).all(): > res.setdefault(hpoint.vehicle_id, []).append({'id':hpoint.id,'location' > : str(hpoint.location)}) > > > > On Tuesday, September 22, 2015 at 6:48:14 PM UTC+3, Johnny W. Santos wrote: >> >> Supose I have the models below, how could I query for a result like this: >> >> [ >> {1: [{"id": 3, "location": "POINT(23.23423423 54.234524234)"},{"id": 4, >> "location": "POINT(23.23423423 54.234524234)"}]}, >> {2: [{"id": 45, "location": "POINT(78.23423423 43.234524234)"},{"id": >> 67, "location": "POINT(34.2347683423 74.234524234)"}]}, >> ] >> >> The keys being the vehicle id and a list of HistoryPoints values. >> >> Supposing I have more fields in HistoryPoint and is prefered to query on >> it due some filter conditions. >> >> >> class Vehicle(Base): >> id = Column(Integer, primary_key=True) >> identifier = Column(Integer) >> >> >> class HistoryPoint(Base): >> vehicle_id = Column(Integer, FokeignKey('vehicles.id')) >> location = Column(Geography('POINT')) >> >> vehicle = relationship('Vehicle', backref='history') >> >> >> I'm really having a hard time on this, any help will be appreciated >> >> Johnny >> > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.