hello. get_queryset question

42 views
Skip to first unread message

Víctor Suárez

unread,
Jan 3, 2016, 9:23:15 PM1/3/16
to Django users
Hello all,
as it's my first post, if any django developer around, nice work! I appreciate the package, I've been able to pull a small project I had in mind for long, very easily with this.

I am needing help with one part of the Views.
I'm using a generic.ListView, nothing fancy, but then, as the list can get really big, I am trying to create a search box for it. I've read around of using get method, which is fine for me, but when I get into the view, and I override get_queryset I never find the kwargs dictionary to work the search.
I also tried overriding get method but I didn't find the args either.
I found a code on stackoverflow which seems to be what I am looking for, but it always return all(), even when django-debug-toolbar tells me the request was made with get data, the View doesn't show up any kwargs

Do anyone has a suggestion? related to both why I am not getting the kwargs and what's the best way to implement a search box on a ListView?
consider I am new to this
Thanks!
regards

---- code ---
    def get_queryset(self):
        try:
            name = self.kwargs['name']
        except:
            name = ''
        if (name != ''):
            object_list = self.model.objects.filter(name__icontains = name)
        else:
            object_list = self.model.objects.all()
        return object_list

---debug ---
View function     Arguments     Keyword arguments     URL name
mymodel.views.MyModelListView   ()  {}  index


Vijay Khemlani

unread,
Jan 3, 2016, 9:28:47 PM1/3/16
to django...@googlegroups.com
Try with

self.request.GET['name']

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2ffc172e-8ae7-4465-a4e3-f8352c067183%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jai Goyal

unread,
Jan 3, 2016, 11:46:58 PM1/3/16
to django...@googlegroups.com
Hello All,

Greetings of New Year !!

I wanted to ask that i have just started web development and i wanted to learn django in deep by making a full dynamic website.

What Should Be the path which i should follow i.e. what are the prerequisites which i have to fulfill to become a full stack Django developer.
and any tips will be apprecieted.


For more options, visit https://groups.google.com/d/optout.



--
Regards,

Jai Goyal
Computer Science, Sophomore
Active Member,Cybros,The LNMIIT,Jaipur

" Don't worry about failure, you only have to be right once - Drew Houston "

James Schneider

unread,
Jan 4, 2016, 12:33:04 AM1/4/16
to django...@googlegroups.com
On Sun, Jan 3, 2016 at 6:28 PM, Vijay Khemlani <vkhe...@gmail.com> wrote:
Try with

self.request.GET['name']

Yes, this is is what you want. 

Normally, though, I've seen it written as self.request.GET.get('name'). This calls the .get() dict method on the GET attribute of the request, which will return None in the event that 'name' does not exist. Calling the member directly above will throw an exception if it doesn't exist, which will likely be the case on the first page load with no filter, unless you always call that view with a URL containing name=, hard to do in Django. 

Also keep in mind that a key may contain multiple values (although it probably won't with a simple search field), you can call .getlist(key) or call .get() multiple times to get all of the values. See the explanation of the GET QueryDict object here:


Self.kwargs refers to the keyword arguments to the view itself within Django, usually consisting of the request and captured URL parameters from your urls.py, among other things.

Admittedly, that's not explicitly spelled out anywhere in the docs AFAIK. 

As an FYI, there are also 3rd party packages that can handle this functionality for you, such as django-filter.

-James

Víctor Suárez

unread,
Jan 4, 2016, 11:16:54 AM1/4/16
to Django users
Thanks!

Víctor Suárez

unread,
Jan 4, 2016, 11:21:29 AM1/4/16
to Django users
Thank you very much

man I can like this framework like a lot.... I end up with:

class MyModel(ListView):
...
        q = self.request.GET.get('q',default='')
        country = self.request.GET.get('country',default='')
        owner = self.request.GET.get('owner',default='')
        self.queryset = CellSite.objects.filter(
                                                 Q(name__icontains=q) |
                                                 Q(code__icontains=q),
                                                 country__istartswith=country,
                                                 owner__istartswith=owner,
                                                 active__exact=True,
                                                    )
        response = super(MyModelListView, self).get(request, *args, **kwargs)
        return response

Reply all
Reply to author
Forward
0 new messages