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])
精彩评论