Registry / web-framework / algoliasearch-django

algoliasearch-django

JSON →
library4.0.0pypypi✓ verified 23d ago

This package provides seamless integration of the Algolia Search API into Django projects. It is currently at version 4.0.0 and is actively maintained, with a focus on compatibility with modern Python and Django versions, built upon the `algoliasearch-client-python` library.

pip install algoliasearch-django
INSTALL
IMPORT
SIG · ALGOLIASEARCH-DJAN
A
algoliasearch-django
web-frameworkpythonv4.0.0
Install
3.5s avg
Import
Disk
66MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v4.0.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 66.4MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 3.5s · import 0.000s · 67MB
66MB installed
● package 66MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

algoliasearch
import algoliasearch_django as algoliasearch
import algoliasearch_django as algoliasearch

To get started, first configure your Algolia credentials in your Django `settings.py` under an `ALGOLIA` dictionary. Then, define your Django models. Finally, create an `index.py` file within your app and register your models for indexing using either `algoliasearch.register()` or the `@register` decorator with a subclass of `AlgoliaIndex` to specify index settings and fields. Remember to add `algoliasearch_django` to your `INSTALLED_APPS`.

import os import django from django.conf import settings from django.db import models # Configure Django settings minimally for standalone execution if not settings.configured: settings.configure( INSTALLED_APPS=[ 'django.contrib.auth', 'django.contrib.contenttypes', 'myapp', 'algoliasearch_django' ], DATABASES={'default': {'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:'}}, ALGOLIA={ 'APPLICATION_ID': os.environ.get('ALGOLIA_APPLICATION_ID', 'YOUR_APP_ID'), 'API_KEY': os.environ.get('ALGOLIA_API_KEY', 'YOUR_API_KEY'), 'INDEX_PREFIX': 'dev_', }, DEFAULT_AUTO_FIELD='django.db.models.AutoField', SECRET_KEY='a-very-secret-key', DEBUG=True ) django.setup() # models.py (example in a fictional 'myapp') class Product(models.Model): name = models.CharField(max_length=255) description = models.TextField() price = models.DecimalField(max_digits=10, decimal_places=2) created_at = models.DateTimeField(auto_now_add=True) def __str__(self): return self.name class Meta: app_label = 'myapp' # index.py (example in 'myapp') from algoliasearch_django import AlgoliaIndex from algoliasearch_django.decorators import register @register(Product) class ProductIndex(AlgoliaIndex): fields = ('name', 'description', 'price', 'created_at') settings = {'searchableAttributes': ['name', 'description'], 'attributesForFaceting': ['price']} # You can customize `get_attribute_name` or `get_extra_attributes` for more complex indexing # --- Example Usage (after defining models and index, typically run via manage.py) --- # This part would typically be run via `python manage.py shell` or a management command. # For demonstration, we simulate creating and saving an object. # In a real Django app, signals handle auto-indexing on save. # Create a product (this would automatically index if AUTO_INDEXING is True, which is default) # from myapp.models import Product # p1 = Product.objects.create(name="Wireless Headphones", description="High-quality audio experience.", price=199.99) # p2 = Product.objects.create(name="Bluetooth Speaker", description="Portable and powerful sound.", price=79.99) # print("Products created and indexed.") # To manually reindex all: python manage.py algolia_reindex # To apply settings: python manage.py algolia_applysettings print("Algolia Django integration configured. To index data, run 'python manage.py algolia_reindex' or save model instances.")
Debug
Known issues
breakingVersion 4.0.0 drops support for Python versions older than 3.8 and Django versions older than 4.0. Ensure your project meets these minimum requirements before upgrading.
fix
Upgrade Python to 3.8+ and Django to 4.0+. Refer to Django's own upgrade guides for framework-specific changes.
affects: 4.0.0 and later
breakingThe `clear_index` method has been removed in favor of `clear_objects` for improved clarity and consistency with the underlying Algolia API client.
fix
Replace all calls to `algoliasearch.clear_index(Model)` with `algoliasearch.clear_objects(Model)`.
affects: 4.0.0 and later
breakingThe configuration for Algolia in `settings.py` has changed. Instead of `ALGOLIA_APPLICATION_ID` and `ALGOLIA_API_KEY` as top-level settings, they are now nested within a single `ALGOLIA` dictionary.
fix
Update your `settings.py` to use `ALGOLIA = {'APPLICATION_ID': 'YOUR_APP_ID', 'API_KEY': 'YOUR_API_KEY'}`.
affects: 4.0.0 and later
gotchaWhen performing searches from the frontend, use the dedicated 'Search-Only API Key' rather than the 'Admin API Key'. The Admin API Key has full write access and should never be exposed client-side.
fix
Distinguish between 'API_KEY' (Admin) for backend operations and 'SEARCH_API_KEY' (Search-Only) for frontend search queries.
affects: All versions
gotchaRecords exceeding Algolia's size limit (typically 10KB) will cause indexing errors. This often occurs when indexing too many fields or large related objects.
fix
Carefully select which fields to index using the `fields` attribute in `AlgoliaIndex`. Consider using `get_extra_attributes` or `get_raw_record` to preprocess and limit the data sent to Algolia, only indexing data relevant for search.
affects: All versions
gotchaIssues can arise with Many-to-Many relationships not updating correctly in the Algolia index, particularly on model saves. Sometimes manual re-indexing is required.
fix
Ensure that related objects are correctly handled, potentially by overriding `save_record` or using custom signals to trigger updates to the main model's index entry when M2M relationships change. Manual re-indexing (`python manage.py algolia_reindex`) might be necessary in some cases.
affects: All versions
Upgrade
Version history
4.0.0latest on PyPI · released Jan 6, 2025
Audit
Dependencies
DjangorequiredCore framework dependency, requires Django>=4.0 for algoliasearch-django v4.
algoliasearchrequiredUnderlying Algolia Python API client, requires algoliasearch>=4.0,<5.0.
Agent activity
46 hits · last 30 days
node
42
OpenAI (training)
1
Resources
algoliasearch-django — pip install algoliasearch-django · libregistry