Django ORM support for the MariaDB Vector field (introduced in MariaDB 11.7+).
MariaDB introduced native vector support, allowing you to store embeddings and perform similarity search directly in the database.
However, Django currently lacks:
- a native
VECTORmodel field - ORM support for vector queries
- automatic migration support for vector indexes
This project fills that gap by providing a clean, Django-native way to work with MariaDB vectors.
- Django
MariaDBVectorFieldforVECTOR(n) - Automatic Django migrations
- Automatic
VECTOR INDEXcreation - ORM-friendly similarity queries
- Recommendation Manager for Django models
- No raw SQL required
- Works with MariaDB 11.8.2+
- Optionally optimization for better performance when working with vectors with use package
orjsonandbinary_responceoption
Without this package, using MariaDB vectors in Django requires:
- manual SQL migrations
- custom query expressions
- fragile schema management
With this package, you get:
- clean Django models
- automatic schema generation
- reusable and maintainable code
This package is useful for:
- Retrieval-Augmented Generation (RAG)
- semantic search
- recommendation systems
- document / code similarity search
Typical workflow: text → embedding → store → similarity search
- Maps Django field → VECTOR(n)
- Extends Django migration system
- Injects MariaDB-specific SQL for:
- vector columns
- vector indexes
- Wraps vector distance functions like:
- VEC_DISTANCE
- VEC_DISTANCE_COSINE
- VEC_DISTANCE_EUCLIDEAN
- Recommendation Manager (bonus)
- Python 3.12+
- Django 5.2+, or 6.0+
- MariaDB 11.8.2 or newer
pip install django-mariadb-vectorFor better performance when working with vectors, install with the orjson extra:
pip install django-mariadb-vector[orjson]models.py:
from django.db import models
from django_mariadb_vector import MariaDBVectorField, MariaDBVectorIndex
class MyModel(models.Model):
embedding = MariaDBVectorField(dimensions=3)
class Meta:
indexes = [
# Vector index (MariaDB 11.8.2+)
MariaDBVectorIndex(fields=["embedding"], dimensions=3)
]You can use VecDistance to perform similarity searches.
from django_mariadb_vector import VecDistance
from .models import MyModel
# Find 5 most similar records to a reference vector
reference_vector = [0.1, 0.2, 0.3]
results = MyModel.objects.annotate(
distance=VecDistance("embedding", reference_vector)
).order_by("distance")[:5]in case of installation django-mariadb-vector library with extra orjson depencedcy
pip install django-mariadb-vector[orjson]or
uv add django-mariadb-vector[orjson]- Up to ~20× faster compared to the standard
jsonlibrary on generate vector data - Up to ~8× faster compared to the standard
jsonlibrary on response vector data
Note: Performance was measured using 20,000 iterations (3 runs) in
tests/test_performance.py, with randomly generated vectors of dimension 3072.
The binary_response option improves performance when working with vectors by returning data in a compact binary format instead of JSON.
- Vectors are returned as a sequence of little-endian IEEE 754 float32 bytes (4 bytes per value)
(MariaDB reference) - Reduces network traffic between MariaDB and your application
- Eliminates JSON parsing overhead on the Python side
- Up to ~16× faster compared to the standard
jsonlibrary - About ~2× faster compared to
orjson - Lower bandwidth usage for large vector payloads
Note: Performance was measured using 20,000 iterations (3 runs) in
tests/test_performance.py, with randomly generated vectors of dimension 3072.
from django.db import models
from django_mariadb_vector import MariaDBVectorField
class MyModel(models.Model):
embedding = MariaDBVectorField(dimensions=3, binary_response=True)Using a RecommendationManager can simplify vector searches in your application:
models.py:
from django.db import models
from django_mariadb_vector import MariaDBVectorField, MariaDBVectorIndex
from django_mariadb_vector.managers import RecommendationManager
class MyModel(models.Model):
embedding = MariaDBVectorField(dimensions=3)
objects = RecommendationManager(vector_field="embedding")
class Meta:
indexes = [
# Vector index (MariaDB 11.8.2+)
MariaDBVectorIndex(fields=["embedding"], dimensions=3, m=16),
]from .models import MyModel
reference_vector:list[float] = [0.1, 0.2, 0.3]
# Find 5 most similar records to a reference vector
results = MyModel.objects.similar_to_vector(reference_vector, limit=5)
for item in results:
print(f"{item.name} - Distance: {item.distance}")from .models import MyModel
reference_id:int = 1
# Find 5 most similar records to a reference object by id
results = MyModel.objects.similar_to(reference_id, limit=5)
for item in results:
print(f"{item.name} - Distance: {item.distance}")The following functions are available in django_mariadb_vector.functions:
Search(expression, vector): Convenient wrapper for COSINE distance search.VecDistance(expression, vector): GenericVEC_DISTANCEfunction.VecDistanceCosine(expression, vector): Native MariaDBVEC_DISTANCE_COSINE.VecDistanceEuclidean(expression, vector): Native MariaDBVEC_DISTANCE_EUCLIDEAN.VecFromText(text): Converts JSON string to MariaDB VECTOR format.VecToText(expression): Converts MariaDB VECTOR format to JSON string.
A minimal demo project showing how to build article recommendations using vector similarity in Django with MariaDB as the database using the django-mariadb-vector library.
- Demo repo: https://github.com/lexxai/django-mariadb-vector-demo
- Example: https://github.com/lexxai/django-mariadb-vector-demo/tree/main/docs
You can test the build and run tests in Docker for different Python versions and OS images.
The Dockerfile is located in tests/docker/Dockerfile.
bash tests/docker/run_docker_tests.shdocker build --build-arg PYTHON_VERSION=3.13 -f tests/docker/Dockerfile -t django-mariadb-vector:test-3.13 .
docker run --rm django-mariadb-vector:test-3.13Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.