#28292: ORing on ManyToMany to same model can result in duplicate items in
queryset
-------------------------------------+-------------------------------------
Reporter: Till | Owner: nobody
Theato |
Type: Bug | Status: new
Component: Database | Version: 1.11
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
One of my models has two ManyToManyFields to another model:
{{{
#!python
class Bar(models.Model):
created = models.DateTimeField(auto_now_add=True)
class Foo(models.Model):
rel1 = models.ManyToManyField('Bar', related_name='foo_rel1')
rel2 = models.ManyToManyField('Bar', related_name='foo_rel2')
}}}
I have to query all instances of Foo that point to the same Bar via any of
the both relationships. However when an instance of Foo had multiple
relationships to unrelated Bar instances, I got duplicated instances in
the resulting queryset:
{{{
#!python
b1 = Bar.objects.create()
b2 = Bar.objects.create()
b3 = Bar.objects.create()
f = Foo.objects.create()
f.rel1.set([b2,b3])
f.rel2.set([b1])
assert Foo.objects.filter(rel2=b1).count() == 1 # Good
assert Foo.objects.filter(rel1=b1).count() == 0 # Good
qs = Foo.objects.filter(Q(rel1=b1) | Q(rel2=b1))
assert qs.count() == 1 # AssertionError, qs.count() == 2
print(qs.values('rel1', 'rel2'))
# <QuerySet [{'rel1': 2, 'rel2': 1}, {'rel1': 3, 'rel2': 1}]>
}}}
As the last output shows, one entry per relationship in rel1 is returned.
--
Ticket URL: <https://code.djangoproject.com/ticket/28292>
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/048.aec087e7f31e77ed84ddf3f7afa7fc27%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.