Tuesday, September 4, 2012

Django admin, trying to do a custom search

I'm saving a JSON parsed string containing dxdiag information in a field and I need to search through this field in the admin interface.
I override queryset function but I can't seem to get it to work. Does someone have any suggestions to what I am doing wrong?

I got 15 posts and when I search for example "windows xp" (with the ") i get "0 results (13 total)", (I have checked and 13 of the 15 has windows xp in them) so it returns all values that contains my search term but nothing shows up in the change_list page.

Code:

class AdminUserData(admin.ModelAdmin):
       search_fields = ['games_played', 'dxdiag,]

  def queryset(self, request):
        qs = super(AdminUserData, self).queryset(request)
        print qs

        if request.GET:          
            query = request.GET.get('q')
            data = query.split(' ')
            begin = []
            end = []
           
            for item in data:
                if item.startswith('"'):
                    begin.append(item[1:])
                   
                if item.endswith('"'):
                    end.append(item[:-1])
           
            if len(begin) == 0 or len(end) == 0:
                return qs
           
            result = []
            for i in xrange(0, len(begin)):
                if begin[i][:-1] == end[i][1:]:
                    result.append(begin[i][:-1])
                else:
                    result.append(begin[i] + ' ' + end[i])
           
            print result
           
            for i in xrange(0, len(result)):
                print i
                print result[i]
                qs = qs.filter(dxdiag__icontains=result[i])
       
        for item in qs:
            print item
        return qs

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/G4k3NxvM94UJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment