The Python Oracle

Filter with multiple conditions on a single related object

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game Looping

--

Chapters
00:00 Question
01:29 Accepted answer (Score 4)
01:46 Answer 2 (Score 3)
02:05 Thank you

--

Full question
https://stackoverflow.com/questions/3834...

Accepted answer links:
[documentation on filter]: https://docs.djangoproject.com/en/1.9/re...

Answer 2 links:
[django docs about Q objects]: https://docs.djangoproject.com/en/3.2/to...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #django #djangoqueryset #djangoorm

#avk47



ACCEPTED ANSWER

Score 4


Edit: What you are saying is not correct. The filter() function automatically 'AND's its parameters. See the documentation on filter




ANSWER 2

Score 3


You can use Q objects to handle more complex queries. See django docs about Q objects

from django.db.models import Q

q.filter(
    Q(answer__code=code) & Q(answer__value=value)
)