views:

136

answers:

1

Hi

I've been using Django for over a year, but I think I've missed out on some very fundamental thing. I have a rather large queryset (1000+ objects) and I'd like to change a single attribute for each of the objects in that queryset. Is this really the way to go? I'm sure there is something simpler?

for obj in qs:
  obj.my_attr = True 
  obj.save()

Thanks

+10  A: 

You can just do the changes in bulk, although this will not fire the Model's save() callbacks:

MyModel.objects.filter(..).update(my_attr=True)

Documentation: Updating multiple objects at once

Paolo Bergantino
Ah, I can't believe I've completely missed the 'update' method! Thanks.
Deniz Dogan
+1 - although I think your "may not" can be changed to "will not", right?
Dominic Rodger
Yup, sorry, I wrote it before I found the documentation. :)
Paolo Bergantino