[Django] #25132: Add get_value() for queryset that gets a single value from a single object

10 views
Skip to first unread message

Django

unread,
Jul 16, 2015, 6:12:46 AM7/16/15
to django-...@googlegroups.com
#25132: Add get_value() for queryset that gets a single value from a single object
----------------------------------------------+--------------------
Reporter: thedrow | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Database layer (models, ORM) | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Easy pickings: 1 | UI/UX: 0
----------------------------------------------+--------------------
This saves me the pain of typing the following code:
foo_pk = MyModel.objects.only('pk').get(name='foo').pk
Now you can type:
foo_pk = MyModel.objects.get_value('pk', name='foo')
Which is more intuitive.

Pull Request at https://github.com/django/django/pull/5004

--
Ticket URL: <https://code.djangoproject.com/ticket/25132>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 16, 2015, 7:04:03 AM7/16/15
to django-...@googlegroups.com
#25132: Add get_value() for queryset that gets a single value from a single object
-------------------------------------+-------------------------------------
Reporter: thedrow | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

I think this could live on a custom queryset just fine (or even as free
method), I cannot remember when I'd ever would have needed something like
this. I know we have a few of "syntax sugar" methods like first and last,
but those are used really often in code and having that as nice oneliners
makes sense.

--
Ticket URL: <https://code.djangoproject.com/ticket/25132#comment:1>

Django

unread,
Jul 16, 2015, 7:54:21 AM7/16/15
to django-...@googlegroups.com
#25132: Add get_value() for queryset that gets a single value from a single object
-------------------------------------+-------------------------------------
Reporter: thedrow | Owner: nobody

Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* easy: 1 => 0


Comment:

I'm not particularly enthusiastic either, but it's better to first propose
new APIs like this on the DevelopersMailingList so that the discussion
involves a wider audience. I did notice that this patterns appears about
25 times in the test suite (`grep -rI "only(" * | grep "get(" | wc -l`),
sometimes `only()` involves multiple fields though.

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

Django

unread,
Jul 16, 2015, 10:27:08 AM7/16/15
to django-...@googlegroups.com
#25132: Add get_value() for queryset that gets a single value from a single object
-------------------------------------+-------------------------------------
Reporter: thedrow | Owner: nobody

Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by charettes):

FWIW this is already achievable with `values_list('pk',
flat=True).get(name='foo')`.

While we can argue the latter is a bit verbose I don't think the use case
is common enough to warrant an addition to the QuerySet API.

--
Ticket URL: <https://code.djangoproject.com/ticket/25132#comment:3>

Django

unread,
Jul 16, 2015, 1:02:41 PM7/16/15
to django-...@googlegroups.com
#25132: Add get_value() for queryset that gets a single value from a single object
-------------------------------------+-------------------------------------
Reporter: thedrow | Owner: nobody

Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by thedrow):

It is worth a documentation change at the very least.

--
Ticket URL: <https://code.djangoproject.com/ticket/25132#comment:4>

Django

unread,
Jul 16, 2015, 1:03:33 PM7/16/15
to django-...@googlegroups.com
#25132: Add get_value() for queryset that gets a single value from a single object
-------------------------------------+-------------------------------------
Reporter: thedrow | Owner: nobody

Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by thedrow):

Also I added a bit of coverage for get(). Should I open a separate PR with
those tests alone?

--
Ticket URL: <https://code.djangoproject.com/ticket/25132#comment:5>

Django

unread,
Jul 16, 2015, 1:16:53 PM7/16/15
to django-...@googlegroups.com
#25132: Add get_value() for queryset that gets a single value from a single object
-------------------------------------+-------------------------------------
Reporter: thedrow | Owner: nobody

Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by timgraham):

I would think `get()` is pretty well tested by now. `tests/basic` seems to
cover the simple stuff and the [http://djangoci.com/job/django-
coverage/HTML_Coverage_Report/ coverage report] doesn't report any
untested lines. Of course, if you think you have something new to offer,
we'll take a look.

--
Ticket URL: <https://code.djangoproject.com/ticket/25132#comment:6>

Django

unread,
Jul 16, 2015, 1:25:35 PM7/16/15
to django-...@googlegroups.com
#25132: Add get_value() for queryset that gets a single value from a single object
-------------------------------------+-------------------------------------
Reporter: thedrow | Owner: nobody

Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by thedrow):

I missed those. The tests are not very well organized for those who are
unfamiliar with their structure. I guess I should try to contribute more
:)
I'm not worry about the time I spent about this method as I learned
something new about Django and because something might come out of it
anyway,

I think we can close this issue and file a new one about documenting what
charettes suggested because it's not something that is either widely known
or intuitive. I expected to get a list/tuple even with flat=True.

--
Ticket URL: <https://code.djangoproject.com/ticket/25132#comment:7>

Django

unread,
Jul 16, 2015, 1:25:45 PM7/16/15
to django-...@googlegroups.com
#25132: Document how to retrieve a single value using values_list() and get()
--------------------------------------+------------------------------------

Reporter: thedrow | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by timgraham):

* has_patch: 1 => 0
* component: Database layer (models, ORM) => Documentation
* easy: 0 => 1
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/25132#comment:8>

Django

unread,
Jul 16, 2015, 1:25:58 PM7/16/15
to django-...@googlegroups.com
#25132: Add get_value() for queryset that gets a single value from a single object
-------------------------------------+-------------------------------------
Reporter: thedrow | Owner: nobody
Type: | Status: closed

Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by thedrow):

* status: new => closed
* component: Documentation => Database layer (models, ORM)


* easy: 1 => 0

* has_patch: 0 => 1
* resolution: => wontfix
* stage: Accepted => Unreviewed


--
Ticket URL: <https://code.djangoproject.com/ticket/25132#comment:9>

Django

unread,
Jul 16, 2015, 1:26:19 PM7/16/15
to django-...@googlegroups.com
#25132: Add get_value() for queryset that gets a single value from a single object
-------------------------------------+-------------------------------------
Reporter: thedrow | Owner: nobody
Type: | Status: new

Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by thedrow):

* status: closed => new
* resolution: wontfix =>


--
Ticket URL: <https://code.djangoproject.com/ticket/25132#comment:10>

Django

unread,
Jul 16, 2015, 1:26:55 PM7/16/15
to django-...@googlegroups.com
#25132: Add get_value() for queryset that gets a single value from a single object
-------------------------------------+-------------------------------------
Reporter: thedrow | Owner: nobody

Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by thedrow):

Sorry about that. Race condition :P

--
Ticket URL: <https://code.djangoproject.com/ticket/25132#comment:11>

Django

unread,
Jul 16, 2015, 1:27:26 PM7/16/15
to django-...@googlegroups.com
#25132: Document how to retrieve a single value using values_list() and get()
--------------------------------------+------------------------------------
Reporter: thedrow | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by thedrow):

* component: Database layer (models, ORM) => Documentation

* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/25132#comment:12>

Django

unread,
Jul 16, 2015, 1:27:49 PM7/16/15
to django-...@googlegroups.com
#25132: Document how to retrieve a single value using values_list() and get()
--------------------------------------+------------------------------------
Reporter: thedrow | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by thedrow):

* has_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/25132#comment:13>

Django

unread,
Jul 16, 2015, 1:28:04 PM7/16/15
to django-...@googlegroups.com
#25132: Document how to retrieve a single value using values_list() and get()
--------------------------------------+------------------------------------
Reporter: thedrow | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by thedrow):

* easy: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/25132#comment:14>

Django

unread,
Jul 16, 2015, 1:28:37 PM7/16/15
to django-...@googlegroups.com
#25132: Document how to retrieve a single value using values_list() and get()
--------------------------------------+------------------------------------
Reporter: thedrow | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by thedrow):

Where do you guys think that this fits? In the queryset guide?

--
Ticket URL: <https://code.djangoproject.com/ticket/25132#comment:15>

Django

unread,
Jul 16, 2015, 2:01:53 PM7/16/15
to django-...@googlegroups.com
#25132: Document how to retrieve a single value using values_list() and get()
--------------------------------------+------------------------------------
Reporter: thedrow | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by timgraham):

Probably either in `ref/models/querysets.txt` under `values_list` or
somewhere in `topics/db/queries.txt`.

--
Ticket URL: <https://code.djangoproject.com/ticket/25132#comment:16>

Django

unread,
Jul 25, 2015, 4:53:14 AM7/25/15
to django-...@googlegroups.com
#25132: Document how to retrieve a single value using values_list() and get()
-------------------------------------+-------------------------------------
Reporter: thedrow | Owner:
Type: | donaldharvey
Cleanup/optimization | Status: assigned

Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by donaldharvey):

* status: new => assigned
* owner: nobody => donaldharvey


--
Ticket URL: <https://code.djangoproject.com/ticket/25132#comment:17>

Django

unread,
Nov 2, 2015, 7:40:15 AM11/2/15
to django-...@googlegroups.com
#25132: Document how to retrieve a single value using values_list() and get()
-------------------------------------+-------------------------------------
Reporter: thedrow | Owner:
Type: | alexmorozov

Cleanup/optimization | Status: assigned
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by alexmorozov):

* owner: donaldharvey => alexmorozov


Comment:

PR is on it`s way.

--
Ticket URL: <https://code.djangoproject.com/ticket/25132#comment:18>

Django

unread,
Nov 2, 2015, 7:47:31 AM11/2/15
to django-...@googlegroups.com
#25132: Document how to retrieve a single value using values_list() and get()
-------------------------------------+-------------------------------------
Reporter: thedrow | Owner:
Type: | alexmorozov
Cleanup/optimization | Status: assigned
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by alexmorozov):

* has_patch: 0 => 1


Comment:

https://github.com/django/django/pull/5534

--
Ticket URL: <https://code.djangoproject.com/ticket/25132#comment:19>

Django

unread,
Nov 3, 2015, 7:24:43 AM11/3/15
to django-...@googlegroups.com
#25132: Document how to retrieve a single value using values_list() and get()
-------------------------------------+-------------------------------------
Reporter: thedrow | Owner:
Type: | alexmorozov
Cleanup/optimization | Status: closed
Component: Documentation | Version: master
Severity: Normal | Resolution: fixed

Keywords: | 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 <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"4373eac99828d99718a7b245d160e7e64fce2e95" 4373eac]:
{{{
#!CommitTicketReference repository=""
revision="4373eac99828d99718a7b245d160e7e64fce2e95"
Fixed #25132 -- Documented how to retrieve a single value using
values_list() and get().
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25132#comment:20>

Django

unread,
Nov 3, 2015, 7:26:22 AM11/3/15
to django-...@googlegroups.com
#25132: Document how to retrieve a single value using values_list() and get()
-------------------------------------+-------------------------------------
Reporter: thedrow | Owner:
Type: | alexmorozov
Cleanup/optimization | Status: closed
Component: Documentation | Version: master
Severity: Normal | Resolution: fixed
Keywords: | 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:"ecb3e314cb0ce4b7fe65e208ea76557443ae6e06" ecb3e314]:
{{{
#!CommitTicketReference repository=""
revision="ecb3e314cb0ce4b7fe65e208ea76557443ae6e06"
[1.9.x] Fixed #25132 -- Documented how to retrieve a single value using
values_list() and get().

Backport of 4373eac99828d99718a7b245d160e7e64fce2e95 from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25132#comment:21>

Django

unread,
Nov 3, 2015, 7:26:28 AM11/3/15
to django-...@googlegroups.com
#25132: Document how to retrieve a single value using values_list() and get()
-------------------------------------+-------------------------------------
Reporter: thedrow | Owner:
Type: | alexmorozov
Cleanup/optimization | Status: closed
Component: Documentation | Version: master
Severity: Normal | Resolution: fixed
Keywords: | 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:"676636ac8f2fa612d68045dd747df7bf2bf8da99" 676636ac]:
{{{
#!CommitTicketReference repository=""
revision="676636ac8f2fa612d68045dd747df7bf2bf8da99"
[1.8.x] Fixed #25132 -- Documented how to retrieve a single value using
values_list() and get().

Backport of 4373eac99828d99718a7b245d160e7e64fce2e95 from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25132#comment:22>

Reply all
Reply to author
Forward
0 new messages