2

Quick question. Cant find it in documentation or rather there are contradictory information.

Does method:

save(update_fields = somefields)

works by the same principal as method:

SomeModel.objects.update(somefields here)

in terms that both methods work on the DB level without triggering SAVE method in the model?

UPDATE works on a DB level, that's clear

What about save(update_fields = somefields)???

Thank you and sorry for rather abstract question


    def delete(self, using=None, keep_parents=False):
        self.show = False
        self.change_date = datetime.datetime.now()
        self.save(update_fields=["show", "change_date"]) # will it trigger save() method in the model or not???

Aleksei Khatkevich
  • 1,923
  • 2
  • 10
  • 27
  • 1
    Maybe this help you https://docs.djangoproject.com/en/2.2/ref/models/instances/#specifying-which-fields-to-save – Higor Rossato May 13 '19 at 15:32
  • 1
    Especially note what happens when the instance was fetched with any deferred fields. `Model.save` does some sort of tricky things which are useful but less obvious than `Manager.update`. While not entirely the same question, [this SO answer](https://stackoverflow.com/a/30453181/2715819) has a lot of useful info. – RishiG May 13 '19 at 15:47
  • 1
    Calling `save` method does trigger the method `save`. And `update_fields` argument is passed to the `pre_save` and `post_save` signals. Any way you call `save` it ends up in `save_base`. `self.save(...) ... will it trigger save() method in the model` **it is** the code triggering `save` method, isn't it? – Ivan Starostin May 13 '19 at 15:57
  • in django docs it says : "Specifying update_fields will force an update". This is not clear.. will it use Manager.update() in this case or it is just standard save() but with a limited set of the fields?. Thing is that datetimefield with auto_add doesnt work with save(update_fields). I just thought that it becouse update_fields works on DB level and in this case there are no signals that might triget auto_add somehow. Just a guess – Aleksei Khatkevich May 13 '19 at 16:03
  • That is in terms of whether _an update or insert must happen_ in the end. There is a `force_update` option. However when `update_fields` is passed - _an update will be forced already_. – Ivan Starostin May 13 '19 at 16:12
  • https://github.com/django/django/blob/master/django/db/models/base.py `if force_insert and (force_update or update_fields): raise ValueError("Cannot force both insert and updating in model saving.")` – Ivan Starostin May 13 '19 at 16:14
  • ok, more or less clear now. Thank you all – Aleksei Khatkevich May 13 '19 at 16:18
  • pre_save should trigger pre-save() method and later used by auto_now ...https://docs.djangoproject.com/en/dev/ref/models/instances/#what-happens-when-you-save,then any type of save() should work correctly with auto_add, hmmm – Aleksei Khatkevich May 13 '19 at 16:35

1 Answers1

1

While using save(update_fields=[.....]) forces an update query in the db level, it is slower than the update() method because an extra call to super.save() is made before that.

super.save(*args, **kwargs)

Instead of using save with update_fields, try using something similar to this.

YourModel.objects.filter(pk=self.pk).update(show=False, change_date=datetime.datetime.now())