Aleksei Khatkevich
Aleksei Khatkevich

Reputation: 2197

question about save(update_fields) method nuances , Django

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???

Upvotes: 2

Views: 2285

Answers (1)

Kishwar Maheen
Kishwar Maheen

Reputation: 26

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())

Upvotes: 1

Related Questions