Opened 8 years ago

Last modified 8 years ago

#27414 closed Cleanup/optimization

Aliasing ForeignKey fields return integer values, not object — at Version 1

Reported by: MikiSoft Owned by: nobody
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 MikiSoft)

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 (1)

comment:1 by MikiSoft, 8 years ago

Description: modified (diff)
Summary: Aliasing ForeignKey fields return integer value, not objectAliasing ForeignKey fields return integer values, not object
Note: See TracTickets for help on using tickets.
Back to Top