#27414 closed Cleanup/optimization (fixed)
Document that aliasing ForeignKey fields in F expressions returns pk values, not model instances
Reported by: | MikiSoft | Owned by: | Jop Reyntjes |
---|---|---|---|
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: | no | UI/UX: | no |
Description (last modified by )
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:
>> 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:
.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. 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.
Change History (7)
comment:1 by , 8 years ago
Description: | modified (diff) |
---|---|
Summary: | Aliasing ForeignKey fields return integer value, not object → Aliasing ForeignKey fields return integer values, not object |
comment:2 by , 8 years ago
Component: | Database layer (models, ORM) → Documentation |
---|---|
Summary: | Aliasing ForeignKey fields return integer values, not object → Document that aliasing ForeignKey fields in F expressions returns pk values, not model instances |
Triage Stage: | Unreviewed → Accepted |
Type: | Bug → Cleanup/optimization |
comment:3 by , 8 years ago
(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.
comment:4 by , 8 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
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.