python - SQLAlchemy group_by return object with aggregated values -
let's consider have such schema:
class owner(db.model): __tablename__ = 'owners' id = db.column(db.integer, nullable=false, primary_key=true, autoincrement=true) name = db.column(db.unicode(30), nullable=false) shares = db.relationship('share', secondary=lambda: shares_owners, lazy="dynamic") class share(db.model): __tablename__ = 'shares' id = db.column(db.integer, nullable=false, primary_key=true, autoincrement=true) apartment = db.column(db.integer, nullable=false) value = db.column(db.integer, nullable=false) owners = db.relationship('owner', secondary=lambda: shares_owners, lazy="dynamic") shares_owners = db.table('shares_owners', db.column('share_id', db.integer, db.foreignkey('shares.id')), db.column('owner_id', db.integer, db.foreignkey('owners.id')) )
filtering shares owned owner piece of cake:
share.query.join(shares_owners, owner, user).filter(user.id == 1).all()
you can group , sum values func.sum
when sqlalchemy returns summed field.
is there way force return share
object (or based on share
) summed value?
ideally call .summed()
method instead of .one()
, .first()
or .all()
return share
object summed values.
Comments
Post a Comment