开发者

Assigning all django models in bulk

W开发者_如何学运维hat is the best method of getting dictionary mapping PK into instances of models, like in_bulk() function, but for all objects of given class(model). I want to minimize the number of SQL queries.


Try dict, and a list comprehension:

model_instances = Model.objects.filter(...)
in_bulk_style_model_instances = dict([(m.pk, m) for m in model_instances])
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜