Saving Only Specific Fields in Django 1.5

By default flushes all fields to the database. While this can be handy in some use cases, more often than not you only need to save a subset of fields. Folks have resorted to various workarounds to deal with this behavior, none of which are very pretty.

Luckily Django 1.5 remedies this situation with the new update_fields argument for save(). This not only gives you a minor performance boost, but can also help with certain race conditions.

One thing to note if you use deferred model loading:

“When saving a model fetched through deferred model loading (only() or defer()) only the fields loaded from the DB will get updated. In effect there is an automatic update_fields in this case. If you assign or change any deferred field value, the field will be added to the updated fields.”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s