python - How to update object returned in query -


so i'm flask/sqlalchemy newbie seems should pretty simple. yet life of me can't work , can't find documentation anywhere online. have complex query run returns me list of database objects.

items = db.session.query(x, func.count(y.x_id).label('total')).filter(x.size >= size).outerjoin(y, x.x_id == y.x_id).group_by(x.x_id).order_by('total asc')\     .limit(20).all() 

after list of items want loop through list , each item update property on it.

for in items:    it.some_property = 'xyz'    db.session.commit() 

however what's happening i'm getting error

it.some_property = 'xyz' attributeerror: 'result' object has no attribute 'some_property' 

i'm not crazy. i'm positive property exist on model x subclassed db.model. query preventing me accessing attributes though can see exist in debugger. appreciated.

class x(db.model):     x_id = db.column(db.integer, primary_key=true)     size = db.column(db.integer, nullable=false)     oords = db.relationship('oords', lazy=true, backref=db.backref('x', lazy='joined'))      def __init__(self, capacity):         self.size = size 

given example result objects not have attribute some_property, exception says. (neither model x objects, hope that's error in example.)

they have explicitly labeled total second column , model x instance first column. if mean access property of x instance, access first result row, either using index, or implicit label x:

items = db.session.query(x, func.count(y.x_id).label('total')).\     filter(x.size >= size).\     outerjoin(y, x.x_id == y.x_id).\     group_by(x.x_id).\     order_by('total asc').\     limit(20).\     all()  # unpack result object x, total in items:    x.some_property = 'xyz'  # please commit after *all* changes. db.session.commit() 

as noted in other answer use bulk operations well, though limit(20) make lot more challenging.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -