#27342: update_or_create example is misleading
-------------------------------+------------------------------
Reporter: kakarukeys | Owner: nobody
Type: Bug | Status: new
Component: Documentation | Version: 1.10
Severity: Normal | Keywords: update_or_create
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------+------------------------------
In the documentation about update_or_create, the following 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()
}}}
is said to be practically the same as:
{{{
obj, created = Person.objects.update_or_create(
first_name='John', last_name='Lennon', defaults=updated_values)
}}}
however, in the former, updated_values is overwritten by John Lennon
dictionary.
whereas, in the latter, John Lennon dictionary is overwritten by
update_values.
suggested code change:
{{{
except Person.DoesNotExist:
new_values = {'first_name': 'John', 'last_name': 'Lennon'}
new_values.update(updated_values)
obj = Person(**new_values)
obj.save()
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27342>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/053.05ec51decc164b86c69abf3164dc00fe%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.