#2361: QuerySet.filter(m2mfield__isnull=False) may return duplicates
-------------------------------------+-------------------------------------
Reporter: daniel.tietze@… | Owner: Norbert
| Stüken
Type: Bug | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Simon Charette):
FWIW the most straightforward way to work around this issue today is to
use `Exists` instead
{{{#!python
Blog.objects.filter(
Exists(Entry.objects.filter(blog=OuterRef("pk"))
)
}}}
Unfortunately the framework doesn't allow you to register transforms on
related fields lookups otherwise this could be as simple as
{{{#!python
Blog.objects.filter(entries__exists=True)
}}}
Here's [https://github.com/django/django/compare/main...charettes:django
:exists-m2m-lookup a very stale and early attempt] at playing this concept
if anyone is interested in trying.
My thoughts at the time was that allowing such transforms could greatly
reduce a lot of the boilerplate associated with multi-valued relationships
and filtering in hope to eventually resolve #2361.
For example
{{{#!python
Blog.objects.filter(entries__exists=Q(published=True))
# Instead of
Blog.objects.filter(
Exists(Entry.objects.filter(blog=OuterRef("pk"), published=True))
)
Blog.objects.filter(entries__count__gte=10)
# Instead of
Blog.objects.annotate(
entries__count=Count("entries")
).filter(entries__count__gte=10)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/2361#comment:23>
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 view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/010701918fba9edf-c72c0cc2-a64e-4f33-abc1-070163e7216b-000000%40eu-central-1.amazonses.com.