{{{
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.
* status: new => assigned
* needs_better_patch: => 0
* needs_tests: => 0
* owner: nobody => Tim Graham
* needs_docs: => 0
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/27342#comment:1>
* has_patch: 0 => 1
Comment:
[https://github.com/django/django/pull/7380 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/27342#comment:2>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"51b83d9e5113ea5b81d04f4d117bd5acd3c1b822" 51b83d9e]:
{{{
#!CommitTicketReference repository=""
revision="51b83d9e5113ea5b81d04f4d117bd5acd3c1b822"
Fixed #27342 -- Corrected QuerySet.update_or_create() example.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27342#comment:3>
Comment (by Tim Graham <timograham@…>):
In [changeset:"9c956ff2f559e7074b5e49449a16857e9c47e3bd" 9c956ff2]:
{{{
#!CommitTicketReference repository=""
revision="9c956ff2f559e7074b5e49449a16857e9c47e3bd"
[1.9.x] Fixed #27342 -- Corrected QuerySet.update_or_create() example.
Backport of 51b83d9e5113ea5b81d04f4d117bd5acd3c1b822 from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27342#comment:5>
Comment (by Tim Graham <timograham@…>):
In [changeset:"aba8f2b5017a95c84bdf4bae9d2e889fc53a9c34" aba8f2b5]:
{{{
#!CommitTicketReference repository=""
revision="aba8f2b5017a95c84bdf4bae9d2e889fc53a9c34"
[1.8.x] Fixed #27342 -- Corrected QuerySet.update_or_create() example.
Backport of 51b83d9e5113ea5b81d04f4d117bd5acd3c1b822 from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27342#comment:4>
Comment (by Tim Graham <timograham@…>):
In [changeset:"e781197d1cc2dcef850104acd0baac1c748d40d7" e781197]:
{{{
#!CommitTicketReference repository=""
revision="e781197d1cc2dcef850104acd0baac1c748d40d7"
[1.10.x] Fixed #27342 -- Corrected QuerySet.update_or_create() example.
Backport of 51b83d9e5113ea5b81d04f4d117bd5acd3c1b822 from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27342#comment:6>