#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 , 9 years ago
comment:2 by , 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 , 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:5 by , 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 , 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 , 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 , 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 object → Document how to retrieve a single value using values_list() and get() |
Triage Stage: | Unreviewed → Accepted |
comment:9 by , 9 years ago
Component: | Documentation → Database layer (models, ORM) |
---|---|
Easy pickings: | unset |
Has patch: | set |
Resolution: | → wontfix |
Status: | new → closed |
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: | Accepted → Unreviewed |
comment:10 by , 9 years ago
Resolution: | wontfix |
---|---|
Status: | closed → new |
comment:12 by , 9 years ago
Component: | Database layer (models, ORM) → Documentation |
---|---|
Summary: | Add get_value() for queryset that gets a single value from a single object → Document how to retrieve a single value using values_list() and get() |
Triage Stage: | Unreviewed → Accepted |
comment:13 by , 9 years ago
Has patch: | unset |
---|
comment:14 by , 9 years ago
Easy pickings: | set |
---|
comment:16 by , 9 years ago
Probably either in ref/models/querysets.txt
under values_list
or somewhere in topics/db/queries.txt
.
comment:17 by , 9 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
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.