{
"$type": "site.standard.document",
"canonicalUrl": "https://rednafi.com/python/save-with-update-fields-in-django/",
"description": "Optimize Django model saves with update_fields parameter to generate leaner SQL queries and improve performance in tight update loops.",
"path": "/python/save-with-update-fields-in-django/",
"publishedAt": "2022-11-09T00:00:00.000Z",
"site": "at://did:plc:fgtm2c26vfcj74rfmeggbyqj/site.standard.publication/3mnl6f7ob462z",
"tags": [
"Python",
"Django",
"TIL",
"Performance"
],
"textContent": "TIL that you can specify update_fields while saving a Django model to generate a leaner\nunderlying SQL query. This yields better performance while updating multiple objects in a\ntight loop. To test that, I'm opening an IPython shell with\npython manage.py shell -i ipython command and creating a few user objects with the\nfollowing lines:\n\nHere's the underlying query Django generates when you're trying to save a single object:\n\nThis will print:\n\nIf you inspect the query, you'll see that although we're only updating the first_name\nfield on the user_0 object, Django is generating a query that updates all the underlying\nfields on the object. The SQL query always passes the pre-existing values of the fields that\nweren't touched. This might seem trivial, but what if the model consisted of 20 fields and\nyou need to call save() on it frequently? At a certain scale the database query that\nupdates all of your columns every time you call save() can start becoming expensive.\n\nSpecifying update_fields inside the save() method can make the query leaner. Consider\nthis:\n\nThis prints:\n\nYou can see this time, Django generates a SQL that only updates the specific field we want\nand doesn't send any redundant data over the wire. The following snippet quantifies the\nperformance gain while updating 1000 objects in a tight loop:\n\nRunning this script will print the following:\n\nYou can see that User.save(updated_fields=[...]) is a tad bit faster than plain\nUser.save.\n\nShould you always use it?\n\nProbably not. While the performance gain is measurable when you're updating multiple objects\nin a loop, it's quite negligible if the object count is low. Also, this adds maintenance\noverhead as any time you change the model, you'll have to remember to keep the\nModel.save(update_fields=[...]) in sync. If you forget to add a field to the\nupdate_fields, Django will silently ignore the incoming data against that field and data\nwill be lost.\n\nFurther reading\n\n- [Specifying which fields to save - Django docs]\n- [Save your Django models using update_fields for better performance - Reddit]\n\n\n\n\n[specifying which fields to save - django docs]:\n https://docs.djangoproject.com/en/4.1/ref/models/instances/#specifying-which-fields-to-save\n\n[save your django models using update_fields for better performance - reddit]:\n https://www.reddit.com/r/django/comments/nynfab/save_your_django_models_using_update_fields_for/",
"title": "Save models with update_fields for better performance in Django"
}