#19843: ORM generate dupes when filtering twice on related objects
----------------------------------------------+--------------------
Reporter: tonnzor | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.4
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
When I apply to a QuerySet a filter for related model twice -- I get
dupes.
Let's say we have classes:
{{{
from django.db import models
class Film(models.Model):
pass
class Person(models.Model):
pass
class PersonInFilm(models.Model):
film = models.ForeignKey(Film)
person = models.ForeignKey(Person)
title = models.CharField(max_length=50)
}}}
Then we try to find out needed PersonInFilm -- multiple filters (e.g.
using Managers):
{{{
qs_wrong =
Person.objects.filter(personinfilm__film=1).filter(personinfilm__role=1)
print "%s rows\n%s" % (qs_wrong.count(), qs_wrong.values('id').query)
}}}
This will result in:
{{{
15 rows
SELECT `persons_person`.`id` FROM `persons_person`
INNER JOIN `persons_personinfilm` ON (`persons_person`.`id` =
`persons_personinfilm`.`person_id`)
INNER JOIN `persons_personinfilm` T4 ON (`persons_person`.`id` =
T4.`person_id`)
WHERE (`persons_personinfilm`.`film_id` = 1 AND T4.`role_id` = 1 )
}}}
Then we try to find out needed PersonInFilm -- single filter:
{{{
qs_correct = Person.objects.filter(personinfilm__film=1,
personinfilm__role=1)
print "%s rows\n%s" % (qs_correct.count(), qs_correct.values('id').query)
}}}
This will result in:
{{{
1 rows
SELECT `persons_person`.`id` FROM `persons_person`
INNER JOIN `persons_personinfilm` ON (`persons_person`.`id` =
`persons_personinfilm`.`person_id`)
WHERE (`persons_personinfilm`.`role_id` = 1 AND
`persons_personinfilm`.`film_id` = 1 )
}}}
Please keep in mind that often it is impossible to put filter in one
command -- e.g. when you use custom Manager.
--
Ticket URL: <https://code.djangoproject.com/ticket/19843>
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].
For more options, visit https://groups.google.com/groups/opt_out.