I’m getting "get_queryset() missing 1 required positional argument: ‘query’ " error. I assume calling the get_queryset function inside a post request function isn’t the way to go about this.
What would be the best way to filter dynamically using Ajax in Django?
The normal calling sequence for get_queryset doesn’t take any parameters. Also keep in mind that ListView by default doesn’t take a POST request, so any change you make to get_queryset needs to be able to handle both cases (GET and POST).
You’ve got a couple of different options:
Create a separate function for this (don’t override get_queryset)
Make the query parameter optional, then check for it being passed in the call
Have get_queryset get the query from self.request.POST, again checking to see if query exists.
Have post save query on self, and have get_queryset reference self.query, again with checking to see that self.query exists.
There are a couple other options but all appear to be basic variations on one or more of these.
Correct, because there’s no post function defined by default within the ListView CBV. You still need that function for the CBV to know it can handle a POST request.
I’m debugging this with Developer Tools in Chrome. Right now it’s returning the entire list of users, not even excluding the admins. When typing something in the input it doesn’t do anything.
I’ve changed the dataType from JSON to HTML in the Ajax, when I print the data on the server side. It correctly prints the value from the input. Still not filtering though.
Interesting, I wouldn’t have thought to approach it from that direction, but that’s ok. What exactly are you getting on the server from self.request.POST.get(‘query’)? (Or, phrasing the same question in a different way, in your post method, what is the value of query being supplied to get_queryset?)
I want to simply fill in my input, capture the value each time the input changes than pass it to the view using AJAX and change the queryset based upon that value. So the results get filtered based upon that value.
The mechanics or frequency doesn’t matter. How / when you invoke the view is a client-side issue and has nothing to do with the Django implementation.
I think you may have a wrong impression of how a CBV works.
The issue is that each time you invoke that view, you need to return the new / filtered queryset. Each call to a CBV creates a new instance of that class. It’s not like your page is repeatedly invoking that method on the same instance of the view. When the view returns a response, that instance of the view class is disposed. Next call, new instance is created. There’s no “memory” or “persistence” of data between views.
But what is the use of passing the new queryset to the success function in Ajax? It won’t change anything, right? Because I won’t do anything with that data inside my success function.
You want to return it back to the calling JavaScript function so that it can update your page. Otherwise, there’s nothing that is going to update the HTML of your list.