89👍
✅
how about …
Photo.objects.filter(complaint__isnull=False)
from https://docs.djangoproject.com/en/dev/topics/db/queries/
23👍
I’m not sure which variant is the best, but that works as well.
Photo.objects.exclude(complaint=None)
Generated SQL query is not exactly like in the case of .filter(complaint__isnull=False)
, but sense is identical.
- [Django]-How do I render jinja2 output to a file in Python instead of a Browser
- [Django]-How to add clickable links to a field in Django admin?
- [Django]-Django: signal when user logs in?
0👍
Depending on the complexity of the relationship and filter logic you might need this (or this can turn out to be more readable):
complaints = Complaint.objects.filter(
# some complex filter here
Q(...) & Q(...) | Q(...)
)
Photo.objects.annotate(
has_complaints=Exists(complaints)
).filter(has_complaints=True)
- [Django]-Django Error u"'polls" is not a registered namespace
- [Django]-How to see which tests were run during Django's manage.py test command
- [Django]-Is it possible to pass query parameters via Django's {% url %} template tag?
Source:stackexchange.com