[Answered ]-Annotating by one-to-many relationship in Django

1👍

If you have an instance of Test1 (saved as test1, for example) and you just want to know if that instance has any related Test2 with isCompleted you could do:

Test2.objects.filter(isCompleted=True, test1=test1.id).exists()

This would return a boolean of whether or not exists a Test2 with isCompleted = True related to your Test1.

If what you want is to get all Test1 that has at least one related Test2 with isCompleted = True, you could do:

Test1.objects.filter(tests__isCompleted=True).distinct()

Leave a comment