Skip to main content

ParadeDB integration for Django

Project description

logo

Bring the full power of ParadeDB into Django.

Build sophisticated, composable search queries effortlessly, and leverage advanced indexing, search, and ranking at blazing speed — all inside PostgreSQL.

📘 See the Documentation

Quick Example

Create a Django model and attach a BM25 index using ParadeDB:

from django.db import models
from django.conf import settings
from django.contrib.postgres.fields import ArrayField
from django.contrib.postgres.fields.ranges import (
    IntegerRangeField,
    DateRangeField,
)
from paradedb.indexes import Bm25Index, IndexFieldConfig, JSONFieldIndexConfig

class Article(models.Model):
    title = models.CharField(max_length=255)
    description = models.TextField()

    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        null=True,
        blank=True,
    )

    created = models.DateTimeField(auto_now_add=True, blank=True)
    rank = models.IntegerField(default=0)
    published = models.BooleanField(default=True)

    price_range = IntegerRangeField(null=True, blank=True, default=None)
    date_range = DateRangeField(null=True, blank=True, default=None)

    metadata = models.JSONField(default=dict, blank=True)
    tags = ArrayField(
        models.TextField(max_length=255),
        default=list,
        blank=True,
    )

    # ParadeDB primary key (optional)
    paradedb_key_field = "id"

    def __str__(self):
        return self.title

    class Meta:
        indexes = [
            Bm25Index(
                "id",
                "title",
                "description",
                "rank",
                "published",
                "price_range",
                "date_range",
                "created",
                "user",
                "metadata",
                name="article_bm25_idx",
                fields_config=IndexFieldConfig(
                    json_fields=[
                        JSONFieldIndexConfig(
                            "metadata",
                            fast=True,
                        )
                    ]
                ),
            ),
        ]

The Bm25Index enables fast, relevance-ranked full-text search.


Basic Full-Text Search

from paradedb.expressions import Search
from .models import Article

results = Article.objects.filter(
    Search("title", "django search")
)

for article in results:
    print(article.title)
  • Inspect query planner to verify:
results.explain()

JSON Field Search

Assume stored JSON:

{
  "category": "backend",
  "framework": "django",
  "reading_time": 2
}

Using Django Lookup (__json_op)

Lookup Pattern

field__json_key__operator__json_op=value
Part Meaning
metadata JSONField
framework JSON key
match, phrase, term, match_conjunction ParadeDB operator
json_op Enables JSON expression
"django" search value

Term

results = Article.objects.filter(
    metadata__framework__term__json_op="django"
)

Using Expression API (JsonOp)

from paradedb.expressions import JsonOp

MATCH

results = Article.objects.filter(
    JsonOp("metadata", "framework", "match", value="dj")
)

Combine with Django Filters

from django.db import models

results = Article.objects.filter(
    JsonOp("metadata", "framwork", "match", value="django") &
    models.Q(rank=2) &
    models.Q(published=True)
)

Mixing ParadeDB Search + ORM

results = Article.objects.filter(
    Search("title", "django") &
    models.Q(rank__gte=5) &
    models.Q(published=True)
)

Aggregation

from paradedb.aggregates import Count, Avg

article_aggregated_count = Article.objects.aggregate(count=Count('id', filter=models.Q(id__all=True)))
print(article_aggregated_count)

avg_read_time_data = Article.objects.aggregate(avg_reading_time=Avg('metadata.reading_time', filter=models.Q(id__all=True)))
print(avg_read_time_data) kw

🎉 You're All Set!

Your Django app is now:

✅ ParadeDB powered ✅ BM25 indexed ✅ JSON searchable

  • To learn more, see the full documentation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

paradedb_django-1.3.0.tar.gz (8.9 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

paradedb_django-1.3.0-py3-none-any.whl (48.2 kB view details)

Uploaded Python 3

File details

Details for the file paradedb_django-1.3.0.tar.gz.

File metadata

  • Download URL: paradedb_django-1.3.0.tar.gz
  • Upload date:
  • Size: 8.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for paradedb_django-1.3.0.tar.gz
Algorithm Hash digest
SHA256 755cd273fc57b2d501a7db03d8bacc9c135ece2643c3ba34f66f2d4342f7d740
MD5 aef66c4a8e43a01c4c206a018260db02
BLAKE2b-256 70d1a400c1c5eb3248afaa264978f98bc7e418105cca66b05efa56166e0c0796

See more details on using hashes here.

File details

Details for the file paradedb_django-1.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for paradedb_django-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 af1e70b077f403275207bddc3a9de10f641fd38114041d1e6678b3cc500d2d19
MD5 bc9df91df67ea4169f262c80a2426f5b
BLAKE2b-256 e0f17b78ecc834ac0f4e22bb21b9baac10e993784adc4471033e61053a392fb7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page