mirror of
https://github.com/django/django.git
synced 2025-08-04 10:59:45 +00:00
Fixed #4102 -- Allow update of specific fields in model.save()
Added the ability to update only part of the model's fields in model.save() by introducing a new kwarg "update_fields". Thanks to all the numerous reviewers and commenters in the ticket
This commit is contained in:
parent
25128856f5
commit
365853da01
8 changed files with 272 additions and 17 deletions
|
@ -135,7 +135,7 @@ Saving objects
|
|||
|
||||
To save an object back to the database, call ``save()``:
|
||||
|
||||
.. method:: Model.save([force_insert=False, force_update=False, using=DEFAULT_DB_ALIAS])
|
||||
.. method:: Model.save([force_insert=False, force_update=False, using=DEFAULT_DB_ALIAS, update_fields=None])
|
||||
|
||||
.. versionadded:: 1.2
|
||||
The ``using`` argument was added.
|
||||
|
@ -289,6 +289,8 @@ almost always do the right thing and trying to override that will lead to
|
|||
errors that are difficult to track down. This feature is for advanced use
|
||||
only.
|
||||
|
||||
Using ``update_fields`` will force an update similarly to ``force_update``.
|
||||
|
||||
Updating attributes based on existing fields
|
||||
--------------------------------------------
|
||||
|
||||
|
@ -334,6 +336,26 @@ For more details, see the documentation on :ref:`F() expressions
|
|||
<query-expressions>` and their :ref:`use in update queries
|
||||
<topics-db-queries-update>`.
|
||||
|
||||
Specifying which fields to save
|
||||
-------------------------------
|
||||
|
||||
.. versionadded:: 1.5
|
||||
|
||||
If ``save()`` is passed a list of field names in keyword argument
|
||||
``update_fields``, only the fields named in that list will be updated.
|
||||
This may be desirable if you want to update just one or a few fields on
|
||||
an object. There will be a slight performance benefit from preventing
|
||||
all of the model fields from being updated in the database. For example:
|
||||
|
||||
product.name = 'Name changed again'
|
||||
product.save(update_fields=['name'])
|
||||
|
||||
The ``update_fields`` argument can be any iterable containing strings. An
|
||||
empty ``update_fields`` iterable will skip the save. A value of None will
|
||||
perform an update on all fields.
|
||||
|
||||
Specifying ``update_fields`` will force an update.
|
||||
|
||||
Deleting objects
|
||||
================
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue