more than one querys LIKE in the same field

2010-12-14 Thread marcoarreguin
Hi friends!

I mean do something like this:

SELECT * FROM table WHERE tags LIKE '%candy%' AND  tags LIKE '%milk%'



I've tried:

table.objects.filter(tags__icontains='candy', tags__icontains='milk')

I've tried too:

list = ['candy', 'milk']
table.objects.filter(tags__icontains=list

And nothing work. Help me please :s

Thanks bros!

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: more than one querys LIKE in the same field

2010-12-14 Thread Chris Lawlor
Not to second guess your intent, but are you sure you don't mean to OR
the two? Your current SQL / Django query will only return tags that
have both 'candy' and 'milk' in the tags string.

If you do want to OR the queries, you can use Q objects:

from django.db.models import Q
table.objects.filter(Q(tags__icontains='milk') |
Q(tags__icontains='candy'))

Also note that, if you're using sqlite, case-insensitive string
lookups are not supported (see 
http://docs.djangoproject.com/en/dev/ref/databases/#sqlite-string-matching).

Hope this is helpful,

Chris

On Dec 14, 3:10 am, marcoarreguin  wrote:
> Hi friends!
>
> I mean do something like this:
>
> SELECT * FROM table WHERE tags LIKE '%candy%' AND  tags LIKE '%milk%'
>
> I've tried:
>
> table.objects.filter(tags__icontains='candy', tags__icontains='milk')
>
> I've tried too:
>
> list = ['candy', 'milk']
> table.objects.filter(tags__icontains=list
>
> And nothing work. Help me please :s
>
> Thanks bros!

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: more than one querys LIKE in the same field

2010-12-14 Thread Łukasz Rekucki
On 14 December 2010 09:10, marcoarreguin  wrote:
> Hi friends!
>
> I mean do something like this:
>
> SELECT * FROM table WHERE tags LIKE '%candy%' AND  tags LIKE '%milk%'
>
>
> I've tried:
>
> table.objects.filter(tags__icontains='candy', tags__icontains='milk')
>
> I've tried too:
>
> list = ['candy', 'milk']
> table.objects.filter(tags__icontains=list
>
> And nothing work. Help me please :s

It's usually more helpful to post errors you receive, so people
reading your message don't have to reproduce them on their own:

>>> print Table.objects.filter(tags__icontains="candy", 
>>> tags__icontains="milk").query
  File "", line 1
SyntaxError: keyword argument repeated

Ok, so we can't repeat keyword arguments in Python. Thankfully, this
isn't the only way to pass query conditions to filter[1]:

>>> print Table.objects.filter(Q(tags__icontains="candy") & 
>>> Q(tags__icontains="milk")).query
SELECT "simple_table"."id", "simple_table"."tags" FROM "simple_table"
WHERE ("simple_table"."tags" LIKE %candy% ESCAPE '\'
  AND "simple_table"."tags" LIKE %milk% ESCAPE '\' )

[1]: 
http://docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q-objects

-- 
Łukasz Rekucki

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.