Opened 8 years ago

Last modified 8 years ago

#27414 closed Cleanup/optimization

Aliasing ForeignKey fields return integer value, not object — at Initial Version

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

Assuming that Relationship consists of from_person and to_person fields, here's an simple example:

>> 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 nor any solution.

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top