On Thursday 03 February 2011 11:10:51 gintare 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?

You can pass kwargs to filter:

query_kwargs = {
   'something__startswith' : 'foobar',
   'somethingelse__gte': 123
}

qs = MyModel.objects.filter(**query_kwargs)

You can pass a sequence:

q_list = [
  Q(foo_bar__icontains='bar'),
  Q(bar_baz=123),
]

qs = MyModel.objects.filter(q_list)

Both creates AND queries. If you need something more complex you can chain Q 
queries:

q_query = Q() # Initialize empty query
q_query &= Q(foobar__icontains='foobar') # AND part
q_query |= Q(baz=123) # OR part

qs = MyModel.objects.filter(q_query)

-- 

Jani Tiainen

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