On Thu, Feb 3, 2011 at 9:10 AM, gintare <g.statk...@gmail.com> wrote:
> Hello,
>
> Could you please advice if it is possible and where i could read OR at
> least that keywords should search for the following question.
>
> Is there a way to submit List =  [Q(word__starts='search_input'),
> Q(date='search_input') ,....]
> to model.objects.filter (List).
> How i have to formulate such list?
>
> My query may contain from 1 to tens of restrictions
> i.e. query with  1 item:
> object. all.filter(Q(word__starts='search_input'))
> versus query with 3 items:
> object. all.filter( Q(word__starts='search_input')  ,
> Q(date='search_input')  ,  Q( smth_else='search_input')   )
>
> I know only programming basics and in order to execute selected
> queries i have to write many if sentences.
> i.e. query with  1 item:
> if (word):  object. all.filter(Q(word__starts='search_input'))
>
> versus query with 3 items:
> if (word & date & smth_else ): object.
> all.filter( Q(word__starts='search_input')  ,
> Q(date='search_input')  ,  Q( smth_else='search_input')   )
>
> Number of combination is very huge. Is there a way to submit a List
> containing [Q(word__starts='search_input'),
> Q(date='search_input') ,....]
> How i have to formulate such list?
>
>
> P.s. In sqlite3 i can make a string in for cycle:
>
> ##    for key in DSearch.keys():
> ##                str2=str2+' ( '+str(key)+' LIKE '+item+')
> AND'
> ## instead of LIKE i use one more if-elif cycle for search_method
> starts, contains, ends, equal
> ##    if (str2!=''):
> ##        str3=str2[:-4]+' );'
> ##        cursor.execute(st3);
> ##        transaction.commit_unless_managed()
> ##        ans=cursor.fetchall()
>
> I would like to use Django.
>

You're using Q objects, did you not read any of the documentation on
them? This is clearly explained[1] in the docs.

qfilter = Q(my_field__contains=search_term)
if search_date:
  qfilter |= Q(date_field=search_term)
# etc.
queryset = MyObject.objects.filter(qfilter)


Cheers

Tom

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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.

Reply via email to