diff --git a/contentcuration/kolibri_public/models.py b/contentcuration/kolibri_public/models.py index 6b20c1f1d7..c0056d9cf9 100644 --- a/contentcuration/kolibri_public/models.py +++ b/contentcuration/kolibri_public/models.py @@ -29,7 +29,15 @@ def has_all_labels(self, field_name, labels): annotations = {} for bitmask_fieldname, bits in bits.items(): annotation_fieldname = "{}_{}".format(bitmask_fieldname, "masked") - filters[annotation_fieldname + "__gt"] = 0 + # To get the correct result, i.e. an AND that all the labels are present, + # we need to check that the aggregated value is euqal to the bits. + # If we wanted an OR (which would check for any being present), + # we would have to use GREATER THAN 0 here. + filters[annotation_fieldname] = bits + # This ensures that the annotated value is the result of the AND operation + # so if all the values are present, the result will be the same as the bits + # but if any are missing, it will not be equal to the bits, but will only be + # 0 if none of the bits are present. annotations[annotation_fieldname] = F(bitmask_fieldname).bitand(bits) return self.annotate(**annotations).filter(**filters)