Sunday, January 3, 2016

Re: hello. get_queryset question



On Sun, Jan 3, 2016 at 6:28 PM, Vijay Khemlani <vkhemlan@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

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/CA%2Be%2BciVtWkKt14YsD_vu-ztnu7syEMM19rn1vOsw9Qgh1W8j7g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment