conversion of budget query to sqlalchemy