I am building a small Task Manager, all Tasks have Labels. I need to Select all tasks which have a few Labels. At the Moment, I am doing:
tasks = Task.objects.all().filter(labels__in=label_list).distinct()
which returns all tasks, where at least one label is also in label_list, but I need only the Tasks that have all labels that are in label_list.
More precise example: If I pass by ['1', '2'] as label_list, I don't want all Tasks with either label 1 OR
label 2 to be returned (that is what is happening now), but want all Tasks with Label 1 AND
Label 2 to be returned.
I want the Tasks which Labels contain label list
The Relation Task-Label is ManyToMany:
class Task(models.Model):
....
labels = models.ManyToManyField(Label, null=True, blank = True)
....