Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h1>Django</h1> <p><a href="https://docs.djangoproject.com/en/dev/ref/models/querysets/#update-or-create" rel="nofollow"><strong>update_or_create</strong></a> (from Django official documentation)</p> <pre><code>update_or_create(defaults=None, **kwargs) </code></pre> <p><em>New in Django 1.7 version.</em></p> <p>A convenience method for updating an object with the given <code>kwargs</code>, creating a new one if necessary. The <code>defaults</code> is a dictionary of (field, value) pairs used to update the object.</p> <p>Returns a tuple of <code>(object, created)</code>, where <code>object</code> is the created or updated object and <code>created</code> is a boolean specifying whether a new object was created.</p> <p>The <code>update_or_create</code> method tries to fetch an object from database based on the given <code>kwargs</code>. If a match is found, it updates the fields passed in the <code>defaults</code> dictionary.</p> <p>This is meant as a shortcut to boilerplatish code. For example:</p> <pre><code>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() </code></pre> <p>This pattern gets quite unwieldy as the number of fields in a model goes up. The above example can be rewritten using <code>update_or_create()</code> like so:</p> <pre><code>obj, created = Person.objects.update_or_create( first_name='John', last_name='Lennon', defaults=updated_values) </code></pre> <p>For detailed description how names passed in kwargs are resolved see <a href="https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.get_or_create" rel="nofollow"><code>get_or_create()</code></a>.</p> <p>As described above in <a href="https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.get_or_create" rel="nofollow"><code>get_or_create()</code></a>, 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.</p> <h1>AppEngine</h1> <p>Not so lucky yet, as Matt has mentioned earlier, it may not be a common use case. Perhaps in AppEngine it's not so much of such use case. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload