Django: change the value of a field for all objects in a queryset

43Tesseracts picture 43Tesseracts · Jan 16, 2016 · Viewed 34.1k times · Source

I have a model MyModel with a boolean field active

Elsewhere, I am retrieving a queryset:

qs = MyModel.Objects.filter(....) 

how can I set active=False for all objects in this qs?

Answer

Pynchia picture Pynchia · Jan 16, 2016

You can update all the records in the queryset with

qs.update(active=False)

Please refer to the official Django documentation for more info