So I am making a query that starts as the following:
queryset = FooBar.objects.filter(example__pk__lt=50)
where example
is a foreign key. So this will get all FooBar
objects that are connected to one of the first fifty Example
s. But here is my goal:
I want to have this queryset only include FooBar
objects where a different field, lets say target
, is not distinct.
I can do that with the following loop:
target_ids = [] #holding a check list for comparison
correct_targets = [] #holding the objects I want to have
for item in queryset: #iteration
target_ids.append(item.example.pk) #send primary key to check list
if target_ids.count(item.example.pk) == 2: #check if there has been more than one (if there is more than two I don't send another correct target since I would have already sent it previously)
correct_targets.append(item) #send correct target
I was hoping that there would be a way to get a queryset of these objects from my queryset
without having to loop through. Is this possible?