Recent entries from each category in django model
<p>I need to show in the template most recent entries from each category is the Instarama, Facebook, Twitter. Here my solution, but it does not work.</p>

<p>My get_queryset method but isn't working:</p>

<pre><code>def get_queryset(self):
    return super(OnlineManager, self).get_queryset().filter(is_online=True).order_by('-date').annotate(Count('social_channel'))[:1]
</code></pre>

<p>This is my model:</p>

<pre><code>class Social(models.Model):
    social_channel = models.CharField(max_length=25, choices=SOCIAL_CHANNELS,
                                      default=SOCIAL_CHANNELS[0][0], blank=True)
    text = models.TextField(max_length=5000, blank=True, default='')
    is_online = models.BooleanField(default=True)
    position = models.PositiveIntegerField(default=0)
    date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.social_channel

    class Meta:
        ordering = ['position']
</code></pre>

<p>Try this:</p>

<pre><code>def get_queryset(self):
    query = super(OnlineManager, self).get_queryset()
    query = query.filter(is_online=True).order_by('social_channel', '-id').distinct('social_channel')
    return query
</code></pre>

<p>it should return the most recent entry for each <code>social_chanel</code></p>

python django model django-queryset categories