1👍
✅
You use the double-underscore syntax, as described further down that page.
Entry.objects.filter(authors__name__startswith='R')
Edit
So what you actually want to do is to exclude all those authors who do not start with R. You could do that with a Q object and the ~
operator:
from django.db.models import Q
Entry.objects.exclude(~Q(authors__name__startswith='R'))
0👍
I can think of a one tricky solution for such a task. (not tested)
Entry.objects \
.annotate(num_authors=Count('authors')) \
.filter(authors__name__startswith='R')
.annotate(num_authors_with_r=Count('authors')) \
.filter(num_authors=F('num_authors_with_r'))
What’s the idea?
- We get the count of all authors.
- Do filter
- Return only rows with filtered authors count == all authors count
- [Answer]-Can't install opentok via pip due to "CompressionError: bz2 module is not available"
- [Answer]-Is my Django profiling middleware wrong?
- [Answer]-How to configure Celery loglevel in Supervisord
- [Answer]-Combine strings from mysql tables without extra spaces
- [Answer]-Django, Django-Angular and the Django Rest Framework
Source:stackexchange.com