This is a basic, but hopefully complete example of an implementation of full text search in Django using PostgreSQL. This can be used as an alternative to Elasticsearch with Haystack, which is a popular solution for this problem. Despite how awesome Elasticsearch is, it might be overkill for many cases.
This requires at least Django 1.10 and it was tested on PostgreSQL 9.6. It assumes that you know your way around both technologies.
Let’s say we have a simple model like this:
And we want to search by first_name, last_name or address.
First, make sure that django.contrib.postgres is in INSTALLED_APPS.
Next, we need a simple form to submit the search terms:
And a very simple template to show the results:
Now, we add the url in urls.py:
Finally, let’s write the View. A simple ListView will do the trick:
Full text search is a complex and fascinating subject, this is only a trivial example.
Django documentation on this subject is here and the excellent PostgreSQL docs are here.