Re: Dynamic queries

26 views
Skip to first unread message

Tom Evans

unread,
Oct 30, 2013, 10:00:43 AM10/30/13
to django...@googlegroups.com
On Wed, Oct 30, 2013 at 1:25 PM, Domagoj Kovač <docto...@gmail.com> wrote:
> Hi guys,
>
> I would like to have dynamic query. For example i have a query like:
>
> rows_list = model_object.objects.not_deleted()
> query = Q(name__contains=search_term)
> query |= Q(equipment_manufacturer__name__contains=search_term)
> rows_list = rows_list.filter(query)
>
> I want to have name__contains dynamic.
>
> My idea to iterate trough model fields and create a query that will perform
> a search, so i can have one function that handles grid searching and search
> trough all the fields in given model.
>
> How can this be done?

query = Q(**{'name_contains': search_term})

and so on.

Cheers

Tom

Tom Evans

unread,
Oct 30, 2013, 10:28:25 AM10/30/13
to django...@googlegroups.com
On Wed, Oct 30, 2013 at 1:25 PM, Domagoj Kovač <docto...@gmail.com> wrote:
> Hi guys,
>
> I would like to have dynamic query. For example i have a query like:
>
> rows_list = model_object.objects.not_deleted()
> query = Q(name__contains=search_term)
> query |= Q(equipment_manufacturer__name__contains=search_term)
> rows_list = rows_list.filter(query)
>
> I want to have name__contains dynamic.
>
> My idea to iterate trough model fields and create a query that will perform
> a search, so i can have one function that handles grid searching and search
> trough all the fields in given model.
>

Further to my earlier answer, you can use that technique to easily
build up dynamic queries, but I'd question the value if you have more
than a few model instances, or many attributes to search. If you
really want to search all fields, use haystack with a solr backend.

Cheers

Tom

Domagoj Kovač

unread,
Oct 30, 2013, 11:20:18 AM10/30/13
to django...@googlegroups.com, teva...@googlemail.com
Thanks Tom, 

I deleted my question because i find an answer in the meantime. I am having a problem with ForeignKey values, it would be cool to search trough them.
Reply all
Reply to author
Forward
0 new messages