开发者

Django: how can I update more than one record at once?

How can I update multiple records in a queryset efficiently?

Do I just loop o开发者_StackOverflow社区ver the queryset, edit , and call save() for each one of them? Is it equivalent to psycopg2's executemany?


If you have to update each record with a different value, then of couse you have to iterate over each record. If you wish to do update them all with the same value, then just use the update method of the queryset.


You can use the queryset update method. Here's the documentation:

http://docs.djangoproject.com/en/dev/ref/models/querysets/#update

If you look at the code, it does loop over the items. In fact this is what Django admin uses when doing "bulk" actions on items.


You can use QuerySet's bulk_update

Example:

YourModel.objects.bulk_update(queryset_or_list_of_objs, ["field1", "field2", ...])
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜