Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django error using pagination and raw queryset

when I try to add pagination to my page it gives me error object of type 'RawQuerySet' has no len() views:

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

how can I solve this?

like image 373
Ali Soltani Avatar asked Jan 17 '26 10:01

Ali Soltani


1 Answers

Both the question and the answer are no longer relevant since Django 2.1 introduced the len() method of RawQuerySet.


You should return the list instead of raw queryset.

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]))
like image 83
numrut Avatar answered Jan 20 '26 00:01

numrut



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!