我需要在模板中显示每个类别的最新条目是Instarama,Facebook,Twitter。在这里我的解决方案,但它不起作用。
我的get_queryset方法但是没有工作:
def get_queryset(self):
return super(OnlineManager, self).get_queryset().filter(is_online=True).order_by('-date').annotate(Count('social_channel'))[:1]
这是我的模特:
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']
答案 0 :(得分:1)
def get_queryset(self):
super(OnlineManager, self).get_queryset().filter(is_online=True).order_by('- id')annotate(Count('social_channel'))[0]
答案 1 :(得分:0)
试试这个:
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
它应该返回每个social_chanel