#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:

> 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.

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:
 {{{#!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. 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 <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.e835416917906d1e365a551e62710e6a%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to