Opened 9 years ago

Closed 8 years ago

Last modified 8 years ago

#25132 closed Cleanup/optimization (fixed)

Document how to retrieve a single value using values_list() and get()

Reported by: Omer Katz Owned by: Alex Morozov
Component: Documentation Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

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

Change History (22)

comment:1 by Florian Apolloner, 9 years ago

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.

comment:2 by Tim Graham, 9 years ago

Easy pickings: unset

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.

comment:3 by Simon Charette, 9 years ago

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.

comment:4 by Omer Katz, 9 years ago

It is worth a documentation change at the very least.

comment:5 by Omer Katz, 9 years ago

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

comment:6 by Tim Graham, 9 years ago

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

comment:7 by Omer Katz, 9 years ago

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.

comment:8 by Tim Graham, 9 years ago

Component: Database layer (models, ORM)Documentation
Easy pickings: set
Has patch: unset
Summary: Add get_value() for queryset that gets a single value from a single objectDocument how to retrieve a single value using values_list() and get()
Triage Stage: UnreviewedAccepted

comment:9 by Omer Katz, 9 years ago

Component: DocumentationDatabase layer (models, ORM)
Easy pickings: unset
Has patch: set
Resolution: wontfix
Status: newclosed
Summary: Document how to retrieve a single value using values_list() and get()Add get_value() for queryset that gets a single value from a single object
Triage Stage: AcceptedUnreviewed

comment:10 by Omer Katz, 9 years ago

Resolution: wontfix
Status: closednew

comment:11 by Omer Katz, 9 years ago

Sorry about that. Race condition :P

comment:12 by Omer Katz, 9 years ago

Component: Database layer (models, ORM)Documentation
Summary: Add get_value() for queryset that gets a single value from a single objectDocument how to retrieve a single value using values_list() and get()
Triage Stage: UnreviewedAccepted

comment:13 by Omer Katz, 9 years ago

Has patch: unset

comment:14 by Omer Katz, 9 years ago

Easy pickings: set

comment:15 by Omer Katz, 9 years ago

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

comment:16 by Tim Graham, 9 years ago

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

comment:17 by Donald Harvey, 9 years ago

Owner: changed from nobody to Donald Harvey
Status: newassigned

comment:18 by Alex Morozov, 8 years ago

Owner: changed from Donald Harvey to Alex Morozov

PR is on it`s way.

comment:20 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: assignedclosed

In 4373eac:

Fixed #25132 -- Documented how to retrieve a single value using values_list() and get().

comment:21 by Tim Graham <timograham@…>, 8 years ago

In ecb3e314:

[1.9.x] Fixed #25132 -- Documented how to retrieve a single value using values_list() and get().

Backport of 4373eac99828d99718a7b245d160e7e64fce2e95 from master

comment:22 by Tim Graham <timograham@…>, 8 years ago

In 676636ac:

[1.8.x] Fixed #25132 -- Documented how to retrieve a single value using values_list() and get().

Backport of 4373eac99828d99718a7b245d160e7e64fce2e95 from master

Note: See TracTickets for help on using tickets.
Back to Top