1👍
✅
You should work with .annotate(…)
to count the number of matching tags:
from django.db.models import Count
article_object = self.object
article_tags_array = article_object.tags.all()
related_articles = self.model.objects.exclude(pk=article_object.pk).filter(
is_published=True,
tags__in=article_tags_array
).annotate(
matching_tags=Count('tags')
).order_by('-matching_tags').prefetch_related()
Source:stackexchange.com