Filter (AND excluding empty values)

278 views
Skip to first unread message

Osiaq

unread,
Dec 13, 2009, 8:46:59 PM12/13/09
to Django users
I'm trying some "simple" stuff and just can't find out the solution:

t=request.GET['city']
c=request.GET['category']
s=request.GET['status']
properties = Property.objects.filter( Q(city=t) | Q(category=c ) | Q
(status=s) )

Goal:
Find all the properties in 'city' no matter of category or status
From the other side: find all the properties that belongs to
'category' no matter of 'status' or 'city' and so on

OR doesn't work properly as well as AND

Andy McKay

unread,
Dec 14, 2009, 12:43:15 AM12/14/09
to django...@googlegroups.com, Osiaq
On 09-12-13 5:46 PM, Osiaq wrote:
> properties = Property.objects.filter( Q(city=t) | Q(category=c ) | Q
> (status=s) )
> Goal:
> Find all the properties in 'city' no matter of category or status

Well you haven't told us what your models look like so we can't really
be sure what you mean. If a Property has a ForeignKey of City, then:
Property.objects.filter(city=t) might work.

> From the other side: find all the properties that belongs to
> 'category' no matter of 'status' or 'city' and so on

Assuming category is meaningful on property this would be:
Property.objects.filter(category=c)...

All I can really say is:
http://docs.djangoproject.com/en/dev/ref/models/querysets/

> OR doesn't work properly as well as AND

If you want help you will need to be more specific. "doesn't work
properly" is not useful information.
--
Andy McKay, @clearwind
Whistler conference: http://clearwind.ca/djangoski/

Osiaq

unread,
Dec 14, 2009, 9:31:29 AM12/14/09
to Django users
That post wasn't clear, my bad.
Again:

t=request.GET['city']
c=request.GET['category']
s=request.GET['status']
properties = Property.objects.filter( Q(city=t) | Q
(category=c ) | Q(status=s) ) #example filter

On the website I've implemented search engine with 3 <select> lists.
CITY,CATEGORY and STATUS

Every list has added:
<option value=0 ...> Select All </option>
So user may chose all properties in CITY, or only HOUSES in CITY, only
HOUSES in all cities or any other combination
How to create filter reading ONLY selected values, without "value=0" ?
In fact, there's more <select> lists and creatiing IF statements for
all of them (with separate filters) might take ages.



Andy McKay

unread,
Dec 14, 2009, 10:58:18 AM12/14/09
to django...@googlegroups.com, Django users
A filter can take a python dictionary. So all you have to do is:

Property.objects.filter(**some_dictionary)

All you have to do is populate that dictionary. You can do that by
accessing request.get, but the best way is to pass that into a django
form.

Osiaq

unread,
Dec 14, 2009, 11:23:45 AM12/14/09
to Django users
Thank you, I'm gonna try this way :)

Osiaq

unread,
Dec 14, 2009, 6:32:56 PM12/14/09
to Django users
Andy, I couldn't manage it. Could you (please) give me some example?
It looks like this case is NOT simple at all, googling through
multiple websites didn't bring anything even close to required
solution.

MODEL:
class Property(models.Model):
name = models.CharField(max_length=200)
category =models.ForeignKey(PropertyCategory)
city =models.ForeignKey(City)
status=models.ForeignKey(PropertyStatus)
def __unicode__(self):
return self.name

VIEW:
def search(request):
q=request.GET['city']
c=request.GET['category']
s=request.GET['status']
properties = Property.objects.filter( HERE_COMES_WHAT_IM_LOOKING_FOR)

HTML:
<form action="/search/" method="get">
<select name="city">
<option value=>ALL</option>
<option value="1">Tokyo</option>
<option value="2">Nashville</option>
</select>

<select name="category">
<option value=>ALL</option>
<option value="1">House</option>
<option value="2">Apartment</option>
</select>

<select name="status">
<option value=>ALL</option>
<option value="1">For Sale</option>
<option value="2">For Rent</option>
</select>

<input type="submit" value="Search">
</form>

...and now I want to display i.e.
all from Tokyo or
all Houses or
all For Sale in Tokyo
etc...




Alex Robbins

unread,
Dec 15, 2009, 9:42:21 AM12/15/09
to Django users
Does it work for one case at a time? Have you tried

q = request.GET.get('city')
properties = Property.objects.filter(city=q)

I would make sure it is working for each variable before combining
them together. (Maybe you already have, I don't know.)

Alex

Osiaq

unread,
Dec 15, 2009, 8:17:26 PM12/15/09
to Django users
Yes, this one is working properly.
Actually I can use i.e

properties = Property.objects.filter( Q(city=t) & Q(category=c ) & Q
(status=s) )

These parameters are working perfect for:
city='Tokio', category='House', status='For Rent'

But fails for:
city='Tokio', category null, status null

Similar filters are working on every single property developer
website, so it must be possible, but how to do it?


Phui Hock

unread,
Dec 15, 2009, 9:26:45 PM12/15/09
to Django users
I suppose you can do something like:
Property.objects.filter(
city=t, Q(category__isnull=True) | Q(category=c), Q
(status__isnull=True) | Q(status=s)
)

Osiaq

unread,
Dec 15, 2009, 11:40:10 PM12/15/09
to Django users
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" ?

Phui Hock

unread,
Dec 16, 2009, 12:17:07 AM12/16/09
to Django users
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)

creecode

unread,
Dec 16, 2009, 12:18:25 AM12/16/09
to Django users
Hello Osiaq,

On Dec 15, 8:40 pm, Osiaq <osiaq.net...@gmail.com> wrote:

> 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" ?

Did you try the exclude method < http://docs.djangoproject.com/en/dev/ref/models/querysets/#exclude-kwargs
>? I haven't been following this closely, just throwing it out there.

Toodle-loooooooooo........
creecode

Osiaq

unread,
Dec 16, 2009, 7:37:59 AM12/16/09
to Django users
@Phui Hock:
YES YES YES !!! You are the One, thank you !!!

@creecode: Thank you for exclude(**kwargs) manual, will have closer
look at it, sounds interesting.

Osiaq

unread,
Aug 21, 2014, 5:44:19 PM8/21/14
to django...@googlegroups.com, phui...@gmail.com

Just to say "thank you" again after 5 years :D But again it saves my ass :D
Reply all
Reply to author
Forward
0 new messages