Is this a bug?

65 views
Skip to first unread message

Luis Masuelli

unread,
Sep 4, 2014, 3:22:29 PM9/4/14
to django-d...@googlegroups.com
I'm opening a django console (to narrow the problem) and run this line:

    for sesion in SesionJuego.objects.filter(preguntas__respuesta__isnull=True): print sesion, sesion.pk

Where:

    * printing is just a sample operation. I'm getting the same error (I'll describe later) with any loop body.
    * SesionJuego is a custom model class, having a `preguntas` attribute which is the reverse side (related_name) of a ForeignKey.
    * I have this issue in an atomic view, but I can also reproduce this error in console.

I have 1 SesionJuego element, with 5 (related) questions in the database.

What I expect is that such loop iterates over ONE element, ONCE. What I get is this (see the console below):

So if I iterate I'm getting a non-unique iteration and the second-to-fifth iterations will yield objects without the related `preguntas` (i.e. for those objects, the populated `preguntas` has a `count` of 0).

Is this a bug? Seems it is iterating over the join instead of over the objects. How can I work in a django-way to avoid that?

Carl Meyer

unread,
Sep 4, 2014, 3:31:52 PM9/4/14
to django-d...@googlegroups.com
Hi Luis,

On 09/04/2014 01:22 PM, Luis Masuelli wrote:
[snip]
> So if I iterate I'm getting a non-unique iteration and the
> second-to-fifth iterations will yield objects without the related
> `preguntas` (i.e. for those objects, the populated `preguntas` has a
> `count` of 0).
>
> Is this a bug? Seems it is iterating over the join instead of over the
> objects. How can I work in a django-way to avoid that?

"Is this a bug?" questions should be asked on the django-users mailing list.

Short answer: no, I don't believe it's a bug, it's expected behavior,
and you can probably fix it by adding .distinct() to your query.

Carl
Reply all
Reply to author
Forward
0 new messages