Code

Opened 3 years ago

Closed 3 years ago

#15494 closed Bug (fixed)

Using only('pk') in related set raises FieldDoesNotExist

Reported by: anonymous Owned by: GDorn
Component: Database layer (models, ORM) Version: 1.2
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
Easy pickings: UI/UX:

Description

This is my first ticket, apologies if this is known or I've missed something in my report.

With model A and model B, where model B has a fk to A:

A().b_set.all().only('pk')

Raises "FieldDoesNotExist: B has no field named 'pk'"

In my example I'm instantiating a new object for brevity. The behaviour is the same with a saved object.

I've attached a simple patch, however I couldn't get the testrunner working, so I couldn't write a full patch. Also, I expect this isn't the best method, but I don't know enough about the ORM to know of a better fix.

Attachments (2)

only_pk_related.diff (846 bytes) - added by sym.roe@… 3 years ago.
only_pk.diff (2.2 KB) - added by GDorn 3 years ago.
Tests and better comments.

Download all attachments as: .zip

Change History (8)

Changed 3 years ago by sym.roe@…

comment:1 Changed 3 years ago by symroe

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 3 years ago by anonymous

  • Needs tests set
  • Patch needs improvement set

comment:3 Changed 3 years ago by russellm

  • Triage Stage changed from Unreviewed to Accepted

comment:4 Changed 3 years ago by GDorn

  • Owner changed from nobody to GDorn
  • Status changed from new to assigned

Changed 3 years ago by GDorn

Tests and better comments.

comment:5 Changed 3 years ago by lrekucki

  • Severity set to Normal
  • Type set to Bug

comment:6 Changed 3 years ago by mtredinnick

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

In [16668]:

Allow "pk" as a field alias in QuerySet.only() calls.

Thanks to GDorn for the patch. Fixed #15494.

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.