开发者

Django update table

开发者 https://www.devze.com 2023-01-10 20:03 出处:网络
obj = Info(name= sub,question=response_dict[\"question\"]) obj.save() After saving the data how to 开发者_如何学Pythonupdate another field of the same table
obj = Info(name= sub,question=response_dict["question"])
obj.save()

After saving the data how to 开发者_如何学Pythonupdate another field of the same table

obj.err_flag=1
obj.update()//Will this work


Just resave that instance:

obj.some_field = some_var
obj.save()

Django automatically knows when to UPDATE vs. INSERT your instance in the database.
This is explained in the Django docs.


obj = Info(name=sub,question=response_dict["question"])
obj.save()

And then later you want to get it and update it (I'm assuming name is unique identifier):

obj = Info.objects.get(name=sub)
obj.err_flag=1
obj.save()


If in the question you mean to say same object or same row where you say same table, then if you do this

obj = Info(name= sub,question=response_dict["question"])
obj.save()

and then after a few lines you need to do this

obj = Info.objects.get(name=sub)
obj.err_flag=1
obj.save()

then obj = Info.objects.get(name=sub) is unnecessary.

You simply do

obj = Info(name= sub,question=response_dict["question"])
obj.save()
#
#do what you want to do, check what you want to check
#
obj.err_flag=1
obj.save()
0

精彩评论

暂无评论...
验证码 换一张
取 消