Django weighted query (annotated values)
<p>I am attempting to created a query and sort it based on a custom calculation of weights.</p>
<p>I require some help as the solution I have come to does indeed work but the performance is not where I'd like it to be</p>
<p>What I have is a Media object. It has related Comments, Likes and Orders.</p>
<p>What currently works but is a complete hacky mess is the following query:</p>
<pre><code>    products = (Media.objects
        .select_related(
            'image',
            'currency',
            'user',
            'user__image',
        )
        .prefetch_related('category', 'tags')
        .exclude(is_deleted=1)
        .filter(Q(category__category__in=categories) | Q(tags__tag__title=query))
        .annotate(order_count = Count('orders', distinct=True))
        .annotate(comment_count = Count('comments', distinct=True))
        .annotate(like_count = Count('likes', distinct=True))
        .annotate(weight = Count(0))
        .distinct())
    
    for m in products.iterator():
        initial_weight  = int(m.order_count)*40 + int(m.comment_count)*4 + int(m.like_count)*4 + int(m.clicks)
        m.weight        = float(float(initial_weight) - float(m.views/50))
</code></pre>
<p>As you can see I am separately annotating all the parameters I'll be using and then doing a stupid iteration full of arithmetic operations for EVERY item in the queryset which is very sub optimal.</p>
<p>One thing I attempted doing was the following:</p>
<pre><code>    products = (Media.objects
        .select_related(
            'image',
            'currency',
            'user',
            'user__image',
        )
        .prefetch_related('category', 'tags')
        .exclude(is_deleted=1)
        .filter(Q(category__category__in=categories) | Q(tags__tag__title=query))
        .annotate(weight = Count('orders', distinct=True) * 40 + Count('comments', distinct=True) * 4 + Count('likes', distinct=True) - F('views')/50 + F('clicks')))
</code></pre>
<p>But similar operations in the annotation were impossible (tried a few variations with and without Sum() - Django always complained that the annotated values were of different type.</p>
<p>By the way we're using django 1.8 for this project.</p>
<p>Is there a good single-query aproach to getting my desired sorting weights?</p>