diff options
| author | Karol Sikora <elektrrrus@gmail.com> | 2013-05-18 13:49:06 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2013-07-12 08:26:35 -0400 |
| commit | 6272d2f155adb4f32ef129d57e9eb5493ebde6ed (patch) | |
| tree | 69f103862be5d5c6a932c0e34c76c59da06cf363 /docs | |
| parent | 66f3d57b79eee0381c29ee4c76582d6b182bfad9 (diff) | |
Fixed #20429 -- Added QuerySet.update_or_create
Thanks tunixman for the suggestion and Loic Bistuer for the review.
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/ref/models/querysets.txt | 46 | ||||
| -rw-r--r-- | docs/releases/1.7.txt | 3 |
2 files changed, 47 insertions, 2 deletions
diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt index d3fe142d36..3963785733 100644 --- a/docs/ref/models/querysets.txt +++ b/docs/ref/models/querysets.txt @@ -1330,7 +1330,7 @@ prepared to handle the exception if you are using manual primary keys. get_or_create ~~~~~~~~~~~~~ -.. method:: get_or_create(**kwargs) +.. method:: get_or_create(defaults=None, **kwargs) A convenience method for looking up an object with the given ``kwargs`` (may be empty if your model has defaults for all fields), creating one if necessary. @@ -1366,7 +1366,6 @@ found, ``get_or_create()`` will instantiate and save a new object, returning a tuple of the new object and ``True``. The new object will be created roughly according to this algorithm:: - defaults = kwargs.pop('defaults', {}) params = dict([(k, v) for k, v in kwargs.items() if '__' not in k]) params.update(defaults) obj = self.model(**params) @@ -1447,6 +1446,49 @@ in the HTTP spec. chapter because it isn't related to that book, but it can't create it either because ``title`` field should be unique. +update_or_create +~~~~~~~~~~~~~~~~ + +.. method:: update_or_create(defaults=None, **kwargs) + +.. versionadded:: 1.7 + +A convenience method for updating an object with the given ``kwargs``, creating +a new one if necessary. The ``defaults`` is a dictionary of (field, value) +pairs used to update the object. + +Returns a tuple of ``(object, created)``, where ``object`` is the created or +updated object and ``created`` is a boolean specifying whether a new object was +created. + +The ``update_or_create`` method tries to fetch an object from database based on +the given ``kwargs``. If a match is found, it updates the fields passed in the +``defaults`` dictionary. + +This is meant as a shortcut to boilerplatish code. For example:: + + try: + obj = Person.objects.get(first_name='John', last_name='Lennon') + for key, value in updated_values.iteritems(): + setattr(obj, key, value) + obj.save() + except Person.DoesNotExist: + updated_values.update({'first_name': 'John', 'last_name': 'Lennon'}) + obj = Person(**updated_values) + obj.save() + +This pattern gets quite unwieldy as the number of fields in a model goes up. +The above example can be rewritten using ``update_or_create()`` like so:: + + obj, created = Person.objects.update_or_create( + first_name='John', last_name='Lennon', defaults=updated_values) + +For detailed description how names passed in ``kwargs`` are resolved see +:meth:`get_or_create`. + +As described above in :meth:`get_or_create`, this method is prone to a +race-condition which can result in multiple rows being inserted simultaneously +if uniqueness is not enforced at the database level. bulk_create ~~~~~~~~~~~ diff --git a/docs/releases/1.7.txt b/docs/releases/1.7.txt index 2cf1cd326e..f3defb37a3 100644 --- a/docs/releases/1.7.txt +++ b/docs/releases/1.7.txt @@ -41,6 +41,9 @@ Minor features * The ``enter`` argument was added to the :data:`~django.test.signals.setting_changed` signal. +* The :meth:`QuerySet.update_or_create() + <django.db.models.query.QuerySet.update_or_create>` method was added. + Backwards incompatible changes in 1.7 ===================================== |
