Technology for ponies

Apr 29, 2013

Save a model without triggering a signal nor the save() method

It happens you may want to save one of your model but without triggering any pre_save / post_save signal, nor the save() method of your model. Reasons could be, your are restoring some fixtures data, you don't want to update the object modification date that is configured with auto_now = True, you want to skip some validations, etc.

There had been a ticket opened in the past to request such a feature for Django but this request had been rejected.

So here is a quick tip to allow you to achieve this behavior if you really have to : you can use django bulk update feature. Because this function only performs a raw SQL query its does not actually knows what object you are updating no signals and no save() methods are triggered / called.

Let say you have the following model with a DateTimeField, auto_now set to True:

class Document(models.Model)
   title = models.CharField(max_length=255)
   last_modified = models.DateTimeField(auto_now=True)

To update the title of a Document without updating his last_modified date you can do:

doc = Document.objects.get(title='Old title')
Document.objects.filter(id=doc.id).update(title='New title')