Search through multiple fields in Django

前端 未结 4 1890
栀梦
栀梦 2021-01-03 00:47

I\'m trying to build a search system, and I want to search by multiple fieldsname, state, city, in my django models. I wrote the below code, yet I\'ve been unab

4条回答
  •  独厮守ぢ
    2021-01-03 01:29

    you can use django Q objects to do OR query,

    or if you want to ANDyour queries together just use the current lookups as kwargs

    seens = Finhall.objects.filter(
      name__icontains=query_string, 
      address__icontains=query_string
    )
    

    You should really consider full text search or haystack (which makes search easy) because icontains issues a %LIKE% which is not remotely scalable

提交回复
热议问题