NBC
NBC

Reputation: 1698

Get_queryset() missing 1 required positional argument: 'request'

I have a listview like this:

class IncidentListView(generic.ListView):
    model = Incident
    paginate_by = 3


    def get_queryset(self,request):
        order_by = request.GET.get('order_by', 'defaultOrderField')
        return model.objects.all().order_by(order_by)  

When I go to the url, I get this error:

get_queryset() missing 1 required positional argument: 'request'

What am I doing wrong? I want to make it so you can click a sorting link that creates a new listview, but unsure how to approach.

For reference, I followed the recommendation here

Upvotes: 0

Views: 3554

Answers (2)

user2390182
user2390182

Reputation: 73470

In class-based views, you can always refer to the request via self.request. But in order to keep all the other magic like pagination working, you should not completely replace the get_queryset method. It is better to override the dedicated hook get_ordering instead. It should return a string or tuple of strings that will be accepted by the model's queryset's order_by:

class IncidentListView(generic.ListView):
    model = Incident
    paginate_by = 3

    def get_ordering(self):
        return self.request.GET.get('order_by', 'defaultOrderField')

Upvotes: 1

Ashish Acharya
Ashish Acharya

Reputation: 3399

Try it like this:

class IncidentListView(generic.ListView):
    model = Incident
    paginate_by = 3


    def get_queryset(self):
        order_by = self.request.GET.get('order_by', 'defaultOrderField')
        return model.objects.all().order_by(order_by) 

Upvotes: 3

Related Questions