tags:

views:

67

answers:

2

How to search all fields in a table in django using filter clause ex:table.object.filter(any field in the table="sumthing")

Thanks.

+3  A: 

I don't think the filter clause is suited to this kind of search. You might want to check out Haystack.

Alasdair
+1 for Haystack - actually implementing it right now.
Daniel Roseman
+1, it sure looks like a job for a full text search mechanism.
celopes
+3  A: 

I agree with Alasdair, but the answer to your question is this though:

from django.db.models import CharField
from django.db.models import  Q

fields = [f for f in table._meta.fields if isinstance(f, CharField)]
queries = [Q(**{f.name: SEARCH_TERM}) for f in fields]

qs = Q()
for query in queries:
    qs = qs | query

table.objects.filter(qs)

note: I have not tested this code, but it should get you quite a bit close to your goal

Jiaaro
Thanks...............................
Hulk
maybe accept an answer? lol
Jiaaro