tags:

views:

331

answers:

2

Is there a wildcard character in Django to use in an Objects.filter?

For example, is there a character that is the equivalent of doing this:

Prices.objects.filter(a = example1,
                      b = example2,
                      #c=example3)

i.e. instead of commenting out c, could I not put c = WILDCARD or c = *... you get the jist. Thank you.

EDIT: As in, if you've got a big list of attributes that can be searched for and you only want to search a select few, you aren't exactly going to have loads of functions that doing those specific searches. I need some kind of character that tells Django and then to SQL "this field doesnt matter, i want everything here"... not including the field (like in the example) just creates a shedload of functions.

+2  A: 

try using contains and icontains.

Here is an example:

Foo.objects.filter(name__icontains = 'hello') #fetches all whose name field contains 'hello'
shanyu
worked like a charm! thank you!
day_trader
you're welcome :)
shanyu
+4  A: 

The only thing to do is a dict of attributes names and values that you dynamically filter with:

filters = {"a": "example1", "b": "example2", "c": "example3" }
prices = Prices.objects.filter(**filters)

Then you set the filters dict at runtime, add or remove key/val pairs as appropriate. That **filters is a keyword argument. Check here for more info:

http://www.nomadjourney.com/2009/04/dynamic-django-queries-with-kwargs/

Gabriel Ross
not quite got it working yet! how does the objects.filter know where to put the keys into the model?
day_trader