Djangonauts, I need to tap your brains.
In a nutshell, I have the following three models:
class Location(models.Model):
name = models.CharField(max_length=100)
class Profile(models.Model):
locations_of_interest = models.ManyToManyField(Location)
class Question(models.Model):
locations = models.ManyToManyField(Location)
I want to find all Profiles which locations of interest intersect with the locations specified for a certain question. That’s easy:
question = Question.objects.first()
matching_profiles = Profile.objects.filter(
locations_of_interest__in=question.locations.all()
)
But in addition, I would also like to know to what extend the locations overlap.
In plain python, I could do something like this:
question_location_names = [l['name'] for l in question.locations.all()]
for profile in matching_profiles:
profile_location_names = [l['name'] for l in profile.locations_of_interest.all()]
intersection = set(question_location_names).intersection(profile_location_names)
intersection_count = len(list(intersection))
# then proceed with this number
However, it seems to me favourable to do the operation directly in the database if possible.
TL;DR
So my question is:
Is there a way to annotate the profile queryset with this intersection count and that way do the operation in the database?
I have tried several things, but I don’t think they are helpful for those who read this and might know an answer.