Search through multiple fields in Django

前端 未结 4 1894
栀梦
栀梦 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:46

    EDIT: Just noticed it is Postgres only

    Apparently in django 1.10 SearchVector class was added.

    Usage from the docs:

    Searching against a single field is great but rather limiting. The Entry instances we’re searching belong to a Blog, which has a tagline field. To query against both fields, use a SearchVector:

    >>> from django.contrib.postgres.search import SearchVector
    >>> Entry.objects.annotate(
    ...     search=SearchVector('body_text', 'blog__tagline'),
    ... ).filter(search='Cheese')
    [, ]
    

提交回复
热议问题