Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » more than one querys LIKE in the same field [RSS Feed]

#1 Dec. 14, 2010 12:49:14

m.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

more than one querys LIKE in the same field


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 =
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 14, 2010 16:02:57

Chris L.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

more than one querys LIKE in the same field


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 (seehttp://docs.djangoproject.com/en/dev/ref/databases/#sqlite-string-matching).

Hope this is helpful,

Chris

On Dec 14, 3:10 am, marcoarreguin <marcoarreg...@gmail.com> 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 =
> 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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Dec. 14, 2010 16:38:36

Łukasz R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

more than one querys LIKE in the same field


On 14 December 2010 09:10, marcoarreguin <marcoarreg...@gmail.com> 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 =
> 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 "<console>", 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:

>>> 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 '\' )

: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 athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » more than one querys LIKE in the same field [RSS Feed]

Board footer

Moderator control

Enjoy the 23rd of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support