How do I get an object and update multiple fields in sqlalchemy

2,773 views
Skip to first unread message

pravin battula

unread,
Aug 18, 2017, 9:41:05 AM8/18/17
to sqlalchemy
Hi,

I'm getting an instance of a model using a primary key like below.
product_live_time = session.query(ProductLiveTime).get(product_id)

Now, i want to update few columns using the same instance product_live_time, how can i do it without doing filter again like below i.e session.query(ProductLiveTime).filter(ProductLiveTime.product_id == product_id).update(data).
is there any other better way of doing it.

Simon King

unread,
Aug 18, 2017, 9:51:12 AM8/18/17
to sqlal...@googlegroups.com
Maybe I'm misunderstanding the question, but:

product_live_item.attr1 = value1
product_live_item.attr2 = value2
session.flush()

Is that what you meant?

Simon

pravin battula

unread,
Aug 18, 2017, 10:14:59 AM8/18/17
to sqlalchemy
The solution which you gave will work but I have a dict of keys to be updated with that get instance. Is there any specific way of updating something like product_live_time_instance.update(data_dict).

Simon King

unread,
Aug 18, 2017, 10:17:50 AM8/18/17
to sqlal...@googlegroups.com
No, but you can trivially write your own function to do it:

def updateobj(obj, data):
for key, value in data.items():
setattr(obj, key, value)

Simon

On Fri, Aug 18, 2017 at 3:14 PM, pravin battula
<pravin....@gmail.com> wrote:
> The solution which you gave will work but I have a dict of keys to be
> updated with that get instance. Is there any specific way of updating
> something like product_live_time_instance.update(data_dict).
>
>
> On Friday, 18 August 2017 19:21:12 UTC+5:30, Simon King wrote:
>>
>> On Fri, Aug 18, 2017 at 2:41 PM, pravin battula
>> <pravin....@gmail.com> wrote:
>> > Hi,
>> >
>> > I'm getting an instance of a model using a primary key like below.
>> > product_live_time = session.query(ProductLiveTime).get(product_id)
>> >
>> > Now, i want to update few columns using the same instance
>> > product_live_time,
>> > how can i do it without doing filter again like below i.e
>> > session.query(ProductLiveTime).filter(ProductLiveTime.product_id ==
>> > product_id).update(data).
>> > is there any other better way of doing it.
>> >
>>
>> Maybe I'm misunderstanding the question, but:
>>
>> product_live_item.attr1 = value1
>> product_live_item.attr2 = value2
>> session.flush()
>>
>> Is that what you meant?
>>
>> Simon
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> 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+...@googlegroups.com.
> To post to this group, send email to sqlal...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
Message has been deleted

Simon King

unread,
Aug 21, 2017, 4:28:43 AM8/21/17
to sqlal...@googlegroups.com
Are you flushing the same session that was used to load the record?
What session does get_instance use?

Simon

On Mon, Aug 21, 2017 at 7:47 AM, pravin battula
<pravin....@gmail.com> wrote:
> I did as below. But didn't work. Am i missing anything else.
>
> def update_record(session, id):
> record_instance = get_instance(id)
> if record_instance:
> updateobj(record_instance, {'time_zone':'GMT','status':'done'})
> session.flush()
> session.commit()
Reply all
Reply to author
Forward
0 new messages