Brian Hicks
Brian Hicks

Reputation: 6403

Does Django generate a DB call on save if values are unchanged?

So say I have Klass as a Django model.

k = Klass.objects.get(id=1)

k.attr1 = k.attr1
k.attr2 = k.attr2
...

k.save()

Assuming the values are all the same, will Django's ORM generate an SQL call?

(My application is batch processing some models in a delayed job. It sets them all from a CSV, and if they're the same values it just saves, currently. I just want to know if I should spend time working on some hash mechanism or logic to detect if they're the same)

Upvotes: 1

Views: 655

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599630

Why didn't you try it in the shell? Use django.db.connection.queries to list all the db operations that have been done in that session.

If you do, you'll see that it does indeed generate a db hit. In fact, you don't even need to set any attributes - just calling save() on a freshly loaded model instance will do the trick. Django model instances don't have any concept of 'dirtiness'.

Upvotes: 3

Related Questions