Django listview 不显示最近添加到数据库的数据
Django listview doesn't show the recent data added to the database
出于某种原因,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']
订购方式:
class Meta:
ordering = ['id']`
将按升序排列您的模型。听起来您希望它们首先列出最近创建的。如果是这种情况,请使用:
class Meta:
ordering = ['-id']`
出于某种原因,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']
订购方式:
class Meta:
ordering = ['id']`
将按升序排列您的模型。听起来您希望它们首先列出最近创建的。如果是这种情况,请使用:
class Meta:
ordering = ['-id']`