def order_by_rating(self, aggregator=models.Sum, descending=True,
                        queryset=None, alias='score'):
        related_field = get_content_object_field(self.model)

        if queryset is None:
            queryset = self.rated_model._default_manager.all()

        ordering = descending and '-%s' % alias or alias

        if not is_gfk(related_field):
            query_name = related_field.related_query_name()

            if len(self.query.where.children):
                queryset = queryset.filter(**{
                    '%s__pk__in' % query_name: self.values_list('pk')
                })

            return queryset.annotate(**{
                alias: aggregator('%s__score' % query_name)
            }).order_by(ordering)

        else:
            return generic_annotate(
                queryset,
                self,
                aggregator('score'),
                related_field,
                alias=alias
            ).order_by(ordering)
dataset/ETHPy150Open django-de/django-simple-ratings/ratings/models.py/RatingsQuerySet.order_by_rating/original