Skip to content
Dave Hall edited this page Sep 11, 2017 · 31 revisions

Getting started with django-watson

To install django-watson, follow these steps:

  1. Install django-watson using pip: pip install django-watson.
  2. Add 'watson' to your INSTALLED_APPS setting.
  3. Update your Database:
    1. Django >=1.7: Run the command manage.py migrate.
    2. Django <1.7: Run the command manage.py syncdb.
  4. Run the command manage.py installwatson.

Existing website data: If you're integrating django-watson with an existing site, then you'll also want to run ./manage.py buildwatson to index your existing data.

Registering models with django-watson

If you want to use django-watson to search your models, you first need to tell it which models to include in it's indexes. To do this, simply call watson.register() for each of the models you want to index. A good place to do this is in your application's AppConfig:

In your_app/apps.py

from django.apps import AppConfig
from watson import search as watson

class YourAppConfig(AppConfig):
    name = "your_app"
    def ready(self):
        YourModel = self.get_model("YourModel")
        watson.register(YourModel)

If file doesn't exist, create it.

In your_app/__init__.py

default_app_config = 'your_app.apps.YourAppConfig'

To learn more about configuring applications, please check the Django documentation.

Note: In Django 1.6 and earlier, you'll need to place your register calls in your models.py file:

from watson import search as watson
watson.register(YourModel)

Once you've registered your models with django-watson, run the command manage.py buildwatson to register any existing models in your database. From now on, any additions, creations or deletions you make will automatically applied, so this command only needs to be run whenever you add a new model to django-watson.

For more information about registering models with django-watson, please visit the registering models wiki page.

Searching for models

You can search for any registered models using the watson.search() method:

from watson import search as watson

search_results = watson.search("Your search text")

for result in search_results:
    print result.title, result.url, result.meta.some_field

result is a watson object. You will not be able to access your object's attribute through it. In case you want to do so:

from watson import search as watson

search_results = watson.filter(Your_model, "Your search text")

for result in search_results:
    print result.your_model_attribute_1, result.your_model_attribute_2
  • Have in mind that this will limit the result/search to one model only. You will have to use watson's object if you want to include several models. Read the documentation bellow for more information.

For more information about searching for models with django-watson, please visit the searching models wiki page.

More information

You can’t perform that action at this time.