[Django] #27342: update_or_create example is misleading

3 views
Skip to first unread message

Django

unread,
Oct 13, 2016, 3:30:17 AM10/13/16
to django-...@googlegroups.com
#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.

Django

unread,
Oct 13, 2016, 8:39:13 AM10/13/16
to django-...@googlegroups.com
#27342: QuerySet.update_or_create() "shortcut to boilerplatish code" example is
incorrect
----------------------------------+--------------------------------------
Reporter: kakarukeys | Owner: Tim Graham
Type: Bug | Status: assigned
Component: Documentation | Version: 1.10
Severity: Normal | Resolution:
Keywords: update_or_create | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
----------------------------------+--------------------------------------
Changes (by Tim Graham):

* 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>

Django

unread,
Oct 13, 2016, 9:02:17 AM10/13/16
to django-...@googlegroups.com
#27342: QuerySet.update_or_create() "shortcut to boilerplatish code" example is
incorrect
----------------------------------+--------------------------------------
Reporter: kakarukeys | Owner: Tim Graham
Type: Bug | Status: assigned
Component: Documentation | Version: 1.10

Severity: Normal | Resolution:
Keywords: update_or_create | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
----------------------------------+--------------------------------------
Changes (by Tim Graham):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/7380 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/27342#comment:2>

Django

unread,
Oct 13, 2016, 11:02:28 AM10/13/16
to django-...@googlegroups.com
#27342: QuerySet.update_or_create() "shortcut to boilerplatish code" example is
incorrect
----------------------------------+--------------------------------------
Reporter: kakarukeys | Owner: Tim Graham
Type: Bug | Status: closed
Component: Documentation | Version: 1.10
Severity: Normal | Resolution: fixed

Keywords: update_or_create | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
----------------------------------+--------------------------------------
Changes (by GitHub <noreply@…>):

* 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>

Django

unread,
Oct 13, 2016, 11:03:56 AM10/13/16
to django-...@googlegroups.com
#27342: QuerySet.update_or_create() "shortcut to boilerplatish code" example is
incorrect
----------------------------------+--------------------------------------
Reporter: kakarukeys | Owner: Tim Graham
Type: Bug | Status: closed
Component: Documentation | Version: 1.10

Severity: Normal | Resolution: fixed
Keywords: update_or_create | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
----------------------------------+--------------------------------------

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>

Django

unread,
Oct 13, 2016, 11:03:57 AM10/13/16
to django-...@googlegroups.com
#27342: QuerySet.update_or_create() "shortcut to boilerplatish code" example is
incorrect
----------------------------------+--------------------------------------
Reporter: kakarukeys | Owner: Tim Graham
Type: Bug | Status: closed
Component: Documentation | Version: 1.10

Severity: Normal | Resolution: fixed
Keywords: update_or_create | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
----------------------------------+--------------------------------------

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>

Django

unread,
Oct 13, 2016, 11:04:13 AM10/13/16
to django-...@googlegroups.com
#27342: QuerySet.update_or_create() "shortcut to boilerplatish code" example is
incorrect
----------------------------------+--------------------------------------
Reporter: kakarukeys | Owner: Tim Graham
Type: Bug | Status: closed
Component: Documentation | Version: 1.10

Severity: Normal | Resolution: fixed
Keywords: update_or_create | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
----------------------------------+--------------------------------------

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>

Reply all
Reply to author
Forward
0 new messages