[Django] #27414: Aliasing ForeignKey fields return integer value, not object

10 views
Skip to first unread message

Django

unread,
Nov 1, 2016, 1:54:06 PM11/1/16
to django-...@googlegroups.com
#27414: Aliasing ForeignKey fields return integer value, not object
-------------------------------------+-------------------------------------
Reporter: MikiSoft | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 1.10
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Assuming that Relationship consists of `from_person` and `to_person`
fields, here's an simple example:
{{{#!python
>> results = Relationship.objects.all().annotate(target=F('to_person'))
>> results[0].from_person
<User: second>
>> results[0].target
3
}}}
So, it doesn't work as expected. It should just rename the field, not
convert it to the integer type.
The same happens with complex annotations when using conditional
expressions (i.e. Case statements). I've even tried like this:
{{{#!python
.annotate(
person= \
Case( \
When( \
to_person__in=friends, \
then=F('to_person') \
), \
default=F('from_person'), \
output_field=ForeignKey('User') \
), \
target= \
Case( \
When( \
to_person__in=friends, \
then=F('from_person') \
), \
default=F('to_person'), \
output_field=ForeignKey('User') \
) \
)
}}}
But it still doesn't work. So that's the reason why I think that this is a
bug, as I haven't found any explanation for this behavior nor any
solution.

--
Ticket URL: <https://code.djangoproject.com/ticket/27414>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 1, 2016, 2:15:49 PM11/1/16
to django-...@googlegroups.com
#27414: Aliasing ForeignKey fields return integer values, not object

-------------------------------------+-------------------------------------
Reporter: MikiSoft | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by MikiSoft:

Old description:

New description:

Assuming that Relationship consists of `from_person` and `to_person`

ForeignKey fields (which point to User), here's an simple example of
showing the actual problem:

bug, as I haven't found any explanation for this behavior. In my case in
aliased fields I would need objects to be returned, not only IDs of them,
therefore I don't know how to solve this.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/27414#comment:1>

Django

unread,
Nov 1, 2016, 3:30:32 PM11/1/16
to django-...@googlegroups.com
#27414: Document that aliasing ForeignKey fields in F expressions returns pk
values, not model instances
--------------------------------------+------------------------------------
Reporter: MikiSoft | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Tim Graham):

* component: Database layer (models, ORM) => Documentation
* type: Bug => Cleanup/optimization
* stage: Unreviewed => Accepted


Comment:

I'm not sure if that can be changed easily, for one, it will be backwards-
incompatible for anyone relying on the current behavior. Feel free to get
other opinions on the DevelopersMailingList. Qualifying as a documentation
change for now.

It would be nice to describe your use case in some more detail to decide
whether or not Django must provide a way for aliasing to return instances.

--
Ticket URL: <https://code.djangoproject.com/ticket/27414#comment:2>

Django

unread,
Nov 3, 2016, 8:35:24 AM11/3/16
to django-...@googlegroups.com
#27414: Document that aliasing ForeignKey fields in F expressions returns pk
values, not model instances
--------------------------------------+------------------------------------
Reporter: MikiSoft | Owner: nobody

Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by Josh Smeaton):

(I lost the original comment I wrote here so here's a condensed version).

Using a ForeignKey as the output_field is an interesting idea that I
hadn't considered, and would solve any backward compatibility issues. I
don't know what the difficulty would be, but F fields already have access
to the query to add necessary joins. I doubt foreign key fields are added
to joins right yet, but __underscore relationships do add joins to the
query. The next hard thing would be reconstructing the object.

All of this sounds doable though.

--
Ticket URL: <https://code.djangoproject.com/ticket/27414#comment:3>

Django

unread,
Nov 5, 2016, 4:19:57 PM11/5/16
to django-...@googlegroups.com
#27414: Document that aliasing ForeignKey fields in F expressions returns pk
values, not model instances
-------------------------------------+-------------------------------------
Reporter: MikiSoft | Owner: Jop
Type: | Reyntjes
Cleanup/optimization | Status: assigned

Component: Documentation | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jop Reyntjes):

* owner: nobody => Jop Reyntjes
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/27414#comment:4>

Django

unread,
Nov 5, 2016, 5:59:29 PM11/5/16
to django-...@googlegroups.com
#27414: Document that aliasing ForeignKey fields in F expressions returns pk
values, not model instances
-------------------------------------+-------------------------------------
Reporter: MikiSoft | Owner: Jop
Type: | Reyntjes
Cleanup/optimization | Status: assigned
Component: Documentation | Version: master

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* has_patch: 0 => 1
* version: 1.10 => master


Comment:

[https://github.com/django/django/pull/7512 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/27414#comment:5>

Django

unread,
Nov 9, 2016, 7:13:25 AM11/9/16
to django-...@googlegroups.com
#27414: Document that aliasing ForeignKey fields in F expressions returns pk
values, not model instances
-------------------------------------+-------------------------------------
Reporter: MikiSoft | Owner: Jop
Type: | Reyntjes
Cleanup/optimization | Status: closed
Component: Documentation | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"f8fab6f90233c7114d642dfe01a4e6d4cb14ee7d" f8fab6f9]:
{{{
#!CommitTicketReference repository=""
revision="f8fab6f90233c7114d642dfe01a4e6d4cb14ee7d"
Fixed #27414 -- Doc'd that F() expressions on relations return pk values.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27414#comment:6>

Django

unread,
Nov 9, 2016, 7:13:37 AM11/9/16
to django-...@googlegroups.com
#27414: Document that aliasing ForeignKey fields in F expressions returns pk
values, not model instances
-------------------------------------+-------------------------------------
Reporter: MikiSoft | Owner: Jop
Type: | Reyntjes
Cleanup/optimization | Status: closed
Component: Documentation | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"89d960121d97a3c2fc8bc768f75bc24f43838c6c" 89d9601]:
{{{
#!CommitTicketReference repository=""
revision="89d960121d97a3c2fc8bc768f75bc24f43838c6c"
[1.10.x] Fixed #27414 -- Doc'd that F() expressions on relations return pk
values.

Backport of f8fab6f90233c7114d642dfe01a4e6d4cb14ee7d from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27414#comment:7>

Reply all
Reply to author
Forward
0 new messages