Thursday, August 21, 2014

Re: Filter (AND excluding empty values)


Just to say "thank you" again after 5 years :D But again it saves my ass :D


On Wednesday, December 16, 2009 7:17:07 AM UTC+2, Phui Hock wrote:
I see. I think this is probably you are looking for:

params = ['city', 'category', 'status']
kwargs = dict([(p, request.GET.get(p)) for p in params if
request.GET.get(p)]) # use just the request params whose value is not
empty string
Property.objects.filter(**kwargs)

On Dec 16, 12:40 pm, Osiaq <osiaq.net...@gmail.com> wrote:
> Well, still the same:
>
> VIEW:
>
> def search(request):
>                 t=request.GET['city']
>                 c=request.GET['category']
>                 s=request.GET['status']
>                 properties =Property.objects.filter(Q(city__isnull=True)|Q(city=t),
> Q(category__isnull=True) | Q(category=c), Q(status__isnull=True) | Q
> (status=s))
>                 return render_to_response ('website/search_results.html',
> {'property': properties, 'city': t})
>
> RESULTS:
> Returning url:http://127.0.0.1:8000/search/?city=1&category=1&status=1
> works perfect
> Returning url:http://127.0.0.1:8000/search/?city=&category=1&status=
> doesnt't work at all (removed 1 from city and status)
>
> Is there anyway to force filter to exclude null values from the
> statement?
> Logic: "if 'city' is null(or 0), exclude it from search term" ?

--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/440bec8a-64ab-44d6-9a94-63092ff771fa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment