gpt4 book ai didi

python - 使用分页和原始查询集的 django 错误

转载 作者:太空宇宙 更新时间:2023-11-04 09:49:03 25 4
gpt4 key购买 nike

当我尝试向我的页面添加分页时,出现错误 object of type 'RawQuerySet' has no len()观点:

class StudentmessageListView(ListView, LoginRequiredMixin):
login_url = '/login/'
redirect_field_name = 'redirect_to'
template_name = 'student_messagesall.html'
context_object_name = 'messages_all'
model = Message
paginate_by = 3

def get_queryset(self):
return Message.objects.raw('SELECT * FROM ertaapp_message where to_prof_id=%s ORDER BY create_date DESC',[self.request.user.id])

def get_context_data(self, **kwargs):
context = super(StudentmessageListView, self).get_context_data(**kwargs)
context['reps'] = ReplyMessage.objects.raw('SELECT * FROM ertaapp_replymessage')
return context

我该如何解决这个问题?

最佳答案

您应该返回列表而不是原始查询集。

def get_queryset(self):
return list(Message.objects.raw('SELECT * FROM ertaapp_message where to_prof_id=%s ORDER BY create_date DESC',[self.request.user.id]))

关于python - 使用分页和原始查询集的 django 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48628163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com