Reputation: 1698
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
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
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