Opened 2 years ago

Closed 23 months ago

Last modified 23 months 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: master
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 Changed 2 years ago by Florian Apolloner

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 Changed 2 years ago by Tim Graham

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 Changed 2 years ago by Simon Charette

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 Changed 2 years ago by Omer Katz

It is worth a documentation change at the very least.

comment:5 Changed 2 years ago by Omer Katz

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

comment:6 Changed 2 years ago by Tim Graham

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 Changed 2 years ago by Omer Katz

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 Changed 2 years ago by Tim Graham

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 Changed 2 years ago by Omer Katz

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 Changed 2 years ago by Omer Katz

Resolution: wontfix
Status: closednew

comment:11 Changed 2 years ago by Omer Katz

Sorry about that. Race condition :P

comment:12 Changed 2 years ago by Omer Katz

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 Changed 2 years ago by Omer Katz

Has patch: unset

comment:14 Changed 2 years ago by Omer Katz

Easy pickings: set

comment:15 Changed 2 years ago by Omer Katz

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

comment:16 Changed 2 years ago by Tim Graham

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

comment:17 Changed 2 years ago by Donald Harvey

Owner: changed from nobody to Donald Harvey
Status: newassigned

comment:18 Changed 2 years ago by Alex Morozov

Owner: changed from Donald Harvey to Alex Morozov

PR is on it`s way.

comment:20 Changed 23 months ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 4373eac:

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

comment:21 Changed 23 months ago by Tim Graham <timograham@…>

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 Changed 23 months ago by Tim Graham <timograham@…>

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