0

I am creating a Django application and trying to include more search boxes in Django web admin interface for a specific model, so that users can simultaniously search more fields. For example, users can enter in one search box the name of the city, and in another search box the name of the street, and all model instances which have corresponding city and street are shown in browser after 'Search' button is hit. Default admin web interface has only one search box. I've added three search boxes in change_list.html file, and when I enter some data in those search boxes in browser and hit 'Search', a proper URL is formed (with query string containing those three input parameters by which the search shoould be done). Then I capture those input parameters in queryset method which I have overriden in my class that extends models.AdminModel calss, perform query set filtering and finally return the filtered queryset. Now, the problem is that the filtered query set is simply not shown on the web page (web interface of my model) after the search process is done, it says that no matches were found. But I am definately sure that my filtered query set contains data beacues I print it in command line just before the return command of queryset method is executed, and it prints correct data.

Here is my queryset method:

def queryset(self, request):

    qs = super(UkopcanjeAdmin, self).queryset(request)
    if "mjesto" in request.GET:
        lokacija = request.GET.get('mjesto',None)
        if lokacija:
            qs = qs.filter(mjesto__icontains=lokacija)

    if "ulica" in request.GET:
        ulica = request.GET.get('ulica',None)
        if ulica:
            qs = qs.filter(ulica__icontains=ulica)

    if "naziv" in request.GET:
        naziv = request.GET.get('naziv',None)
        if naziv:
            qs = qs.filter(naziv__icontains=naziv)

    print qs #this prints correct filtered data
    return qs

Why isn't filtered query set shown in web interface?

EDIT : Thanks to my friend, I've finally managed to solve the problem. And solution is quite simple, all I had to do is move the whole code of queryset method to get_search_results method, and along filtered queryset return false boolean parameter (because get_search_results method returns two parameters). So if anybody wants to customize the search process in Django, the get_search_results method should be used. There you can access query string argumnets and retreive some data the way you want.

1 Answer 1

1

I think it should be get_queryset but not queryset method.

def get_queryset(request):
   #your code here
   queryset = super(UkopcanjeAdmin, self).get_queryset(request)
   if 'miesto' in request.GET:
       print 'Yes'
       return queryset
   else:
       print 'No'

Here is some short explanation from Django docs.

3
  • But can I access URL parameters in get_queryset method?
    – programmer
    Nov 27, 2014 at 15:59
  • @programmer I have updated my answer. It take 'request' as argument and you can acces GET and all other that you need from there.
    – wolendranh
    Nov 27, 2014 at 16:03
  • I've changed queryset to get_queryset, but unfortunately behaviour is the same. Filtered query set contains correct data, but it is simply not displayed in browser... It is interesting that it works well only if I have one parameter in my URL query string, for example localhost:8000/admin/mainApp/some_model/?location=someCity, but if I have more than one parameter, filtered query set is not displayed...
    – programmer
    Nov 28, 2014 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.