Django order by highest number of likes
<p>I'm trying to create a page where people can see the highest rated article, there is one problem: when I filter the number of likes of an article that was also liked by another user it creates a copy of the upvoted article.</p>

<p><a href="https://i.stack.imgur.com/Z3FNd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Z3FNd.png" alt="enter image description here"></a></p>

<p>What I want is to order the articles of the blog by the highest number of likes. </p>

<p><strong>models.py</strong></p>

<pre><code>class Article(models.Model):
    user = models.ForeignKey(User, default='1')
    [... unrelated fields ...]
    likes = models.ManyToManyField(User, related_name="likes")

    [... unrelated function ...]
</code></pre>

<p><strong>views.py</strong></p>

<pre><code>def article_ordered_by_likes(request):
    context = {'article': Article.objects.order_by('-likes')}
    return render(request, 'article_ordered_by_likes.html', context)

def like_button(request):
    if request.method == 'POST':
        user = request.user
        id = request.POST.get('pk', None)

        article = get_object_or_404(Article, pk=id)

        if article.likes.filter(id=user.id).exists():
            article.likes.remove(user)
        else:
            article.likes.add(user)

        context = {'likes_count': article.total_likes}
    return HttpResponse(json.dumps(context), content_type='application/json')
</code></pre>

<p><strong>article_ordered_by_likes.html</strong></p>

<pre><code>{% for a in article %}
    [... unrelated html ...]
    &lt;h2&gt;{{ a.titre }}&lt;/h2&gt;
    &lt;span id="count{{ a.id }}"&gt;{{ a.total_likes }}&lt;/span&gt;
    &lt;input type="button" class="like" id="{{ a.id }}" value="Like" /&gt;
{% endfor %}
</code></pre>

<p>Why is Django creating the same post multiple times? How can I order the articles by the highest number of likes without having this issue?</p>