Django listview不显示最近添加到数据库中的数据

问题描述 投票:1回答:1

出于某种原因,Listview不会检索要添加到数据库中的新鲜数据。这是我的代码:

class UserThreadsListView(ListView):
    model = Thread
    template_name = 'tweetsview.html'
    paginate_by = 20
    context_object_name = 'threads_list'

    def get_context_data(self, **kwargs):
        context =  super(UserThreadsListView, self).get_context_data(**kwargs)
        responsible = ConciergeSpecialist.objects.get(id=self.kwargs['pk'])
        if self.request.user.is_superuser:
            context['teammates_list'] = ConciergeSpecialist.objects.all().exclude(active=False)
        else:
            context['teammates_list'] = ConciergeSpecialist.objects.filter(Q(org_unit_name=self.request.user.org_unit_name) & Q(active=True))
        context['responsible'] = responsible
        return context

    def get_queryset(self):
        responsible = ConciergeSpecialist.objects.get(id=self.kwargs['pk'])
        queryset = super(UserThreadsListView, self).get_queryset()
        return queryset.filter(tweet__responsible=responsible).order_by('id', '-tweet__created_at').distinct('id')

然后,我使用{% for thread in threads_list %}遍历模板tweetsview.html中的线程。

我可以在数据库中看到数据,但是由于某种原因,它没有被检索到模板中。只有第一次检索的旧数据才能正确显示在模板中。我该如何解决?

我的线程模型

class Thread(models.Model):

    name = models.CharField(max_length=50, null=True, blank=True)

    class Meta:
        ordering = ['id']
django python-3.x django-models django-rest-framework django-queryset
1个回答
0
投票

订购:

class Meta:
    ordering = ['id']`

将按照升序对模型进行排序。听起来您想让它们首先列出最近创建的。如果是这种情况,请使用:

class Meta:
    ordering = ['-id']`
© www.soinside.com 2019 - 2024. All rights reserved.