- Django 2 by Example
- Antonio Melé
- 78字
- 2021-06-25 21:19:07
Simple search lookups
Edit the settings.py file of your project and add django.contrib.postgres to the INSTALLED_APPS setting, as follows:
INSTALLED_APPS = [
# ...
'django.contrib.postgres',
]
Now, you can search against a single field using the search QuerySet lookup, like this:
from blog.models import Post
Post.objects.filter(body__search='django')
This query uses PostgreSQL to create a search vector for the body field and a search query from the term django. Results are obtained by matching the query with the vector.