开发者

append item in for loop

mysql db table XXX

id | a  |
---------
1  | 111|
2  | 222|
3  | 333|

I got query result from mysql database.

info = XXX.objects.filter(a = 111)
for item 开发者_如何转开发in info:
    item.update({b:111})

It makes error "XXX has no attribute 'update' I want to append more key:value set into item. How can I go it?


if your table has field b

XXX.objects.filter(a = 111).update(b=111)

or

info = XXX.objects.filter(a = 111)
for item in info:
    item.b = 111
    item.save()


item is not a mapping. If you want to set attributes on an object then just do so.

item.b = 111

Note that Django will not retain the change unless it is already a field in the model and you make sure to save item.


In Django filter return the list of QuerySet not dictionary so @Danfi's approach is right

info = XXX.objects.filter(a = 111) #return the list of QuerySet (objects)
for item in info:
    item.b = 111   # Object
    item.save()
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜