algoliasearch-django package connects your Django models to Algolia.
It uses Django signals to update your Algolia index when you create, update, or delete records.
It also adds a raw_search function to query your indices.
The package builds on the Algolia API client for Python.
The package is open source and available on GitHub.
For a sample Django app with Autocomplete and InstantSearch results pages, see the algoliasearch-django-example repository.
Before you begin
You need:- An Algolia account
- Django 4 or 5
- A Python version supported by your Django release
Set up the package
Set up the package in a new Django project. Then index and search a record.1
Create a Django project
To create a new Django project with a
contacts app, run:2
Install the package
To install the package and the Algolia API client for Python, run:
3
Configure the package
In Replace
algolia_demo/settings.py, add algoliasearch_django and contacts to INSTALLED_APPS.
Then add the ALGOLIA setting:algolia_demo/settings.py
ALGOLIA_APPLICATION_ID and ALGOLIA_API_KEY with your Algolia credentials.
Find both in the Algolia dashboard.
To run this test, use an API key with the search and addObject ACLs.4
Create a model
To create a To create the database table, generate and apply the migration, run:
Contact model and its database table,
replace the contents of contacts/models.py:contacts/models.py
5
Connect the model to Algolia
To send your model to Algolia, register it in an Replace
index.py file in your app.
The package finds this file when Django starts.
It sets up your and options.
An empty subclass of AlgoliaIndex sets up the default options.Create contacts/index.py:contacts/index.py
INDEX_NAME with the name of a new test index.
Without index_name, the package uses the model name.6
Index and search a record
To add a record and search for it, run:If the setup works, the command prints
['Jon Doe'].
Each write waits for Algolia to finish indexing, so the record is searchable right away.
Each run adds another record to your test index.
To remove the indexed test records,
delete the index in the Algolia dashboard or with the Algolia CLI.
This also deletes the index settings.
The test contacts are stored separately in your Django database.Use optional settings
BesidesAPPLICATION_ID and API_KEY, the ALGOLIA setting accepts these optional properties:
-
INDEX_PREFIXA prefix for all indices. Use it to separate different indices, likesite1_Productsandsite2_Products. -
INDEX_SUFFIXA suffix for all indices. Use it to differentiate development and production environments, such asLocation_devandLocation_prod. -
AUTO_INDEXINGIf true (default), models are automatically synced with Algolia. -
RAISE_EXCEPTIONSRaise exceptions for network errors instead of logging them (defaults to the value ofsettings.DEBUG).
Configure your index
Algolia indexes all model fields by default. To change which fields you index, or to set index settings, add attributes to yourAlgoliaIndex subclass:
Python
register as a function:
Python
Next steps
- Commands: reindex, clear, and apply settings from the command line
- Search: build a search experience for your Django app
- Troubleshooting: fix connection and indexing errors