Code

Opened 3 years ago

Closed 3 years ago

#15546 closed Bug (fixed)

Fields selected via extra() are omitted in ValuesQuerySet when fields are specified

Reported by: jnns Owned by: nobody
Component: Documentation Version: 1.3-beta
Severity: Normal Keywords: extra valuesqueryset
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Books.objects.values().extra(select={'price_per_page': 'price / page'}) works fine.

Books.objects.values('book_id').extra(select={'price_per_page': 'price / page'}) however, does not.

Attachments (0)

Change History (8)

comment:1 Changed 3 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

Working as designed. You've explicitly specified the values you want to be returned; if you want the extra column to be returned, you need to include it in the values() clause.

comment:2 Changed 3 years ago by jnns

Correct me if I'm wrong but don't the docs say the opposite?

If you use a values() clause after an extra() clause, any fields defined by a select argument in the extra() must be explicitly included in the values() clause. However, if the extra() clause is used after the values(), the fields added by the select will be included automatically.

http://docs.djangoproject.com/en/dev/ref/models/querysets/#values

comment:3 Changed 3 years ago by russellm

  • Resolution invalid deleted
  • Status changed from closed to reopened

Hrm. This requires further investigation. I was fairly certain that values + extra was fairly extensively tested (see the extra_regress regression tests), so either the documentation is incorrect, or the tests are incomplete or incorrect.

comment:4 Changed 3 years ago by russellm

  • Component changed from Database layer (models, ORM) to Documentation
  • Triage Stage changed from Unreviewed to Accepted

Ok - this looks like a failure of documentation. The current behavior has been tested since at least the 1.0 branch, so the documentation hasn't reflected reality for some time.

comment:5 Changed 3 years ago by jnns

Maybe I should be asking this on the django-developers list but what is the rationale behind ignoring the extra-fields after a values() clause?

As values() has a grouping function when used in aggregation, it would be quite handy if additional fields could be added with extra().

comment:6 Changed 3 years ago by lukeplant

  • Type set to Bug

comment:7 Changed 3 years ago by lukeplant

  • Severity set to Normal

comment:8 Changed 3 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from reopened to closed

In [16697]:

Corrected explanation of values() and extra() interaction in querysets.

Fixes #15546, refs #13455. The original documentation patch
inadvertently muddied the waters in one aspect and this commit tidies
that up.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.