I need to re-save all my models
Users.objects.all().update(active=True)
In the model:
def save(self, *args, **kwargs):
self.url = "XYZ"
super(User, self).save(*args, **kwargs)
However, the above does not trigger the save()
method on these model. So no url = XYZ is set. Is this possible?
Update:
Looks like I cannot do this using .update() I have tried this:
>>> objects = Users.objects.all()
>>> for item in objects:
... item.save()
No, this is impossible:
Realize that update() does an update at the SQL level and, thus, does not call any save() methods on your models, nor does it emit the pre_save or post_save signals (which are a consequence of calling Model.save()). If you want to update a bunch of records for a model that has a custom save() method, loop over them and call save()
$ ./manage.py shell
Python 2.7.6 (default, Mar 22 2014, 22:59:38)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.contrib.auth.models import User
>>> for u in User.objects.all():
... u.is_active = True
... u.save()
...
>>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With