Django ORM support for MariaDB Vector field (MariaDB 11.8.2+)
Project description
Django MariaDB Vector
Django ORM support for the MariaDB Vector field (introduced in MariaDB 11.7+).
Why this project exists
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.
Features
- 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
Why use this
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
Use cases
This package is useful for:
- Retrieval-Augmented Generation (RAG)
- semantic search
- recommendation systems
- document / code similarity search
Typical workflow: text → embedding → store → similarity search
How it works
- 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)
Requirements
- Python 3.12+
- Django 5.2+, or 6.0+
- MariaDB 11.8.2 or newer
Installation
pip install django-mariadb-vector
Optional: faster JSON serialization
For better performance when working with vectors, install with the orjson extra:
pip install django-mariadb-vector[orjson]
Usage
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)
]
Querying with Vector Functions
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]
Optimization: orlson for better performance
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]
Benefits orlson vs json (batch mode)
- 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.
Optimization: binary_response for better performance
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
Benefits of binary response (batch mode)
- 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.
Usage of binary_response
from django.db import models
from django_mariadb_vector import MariaDBVectorField
class MyModel(models.Model):
embedding = MariaDBVectorField(dimensions=3, binary_response=True)
Recommendation Manager for Django models
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),
]
Example of usage Manager
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}")
Advanced Functions
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.
Reference
Demo
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
Docker Testing
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.
Using the helper script (Linux/macOS)
bash tests/docker/run_docker_tests.sh
Manual build example
docker 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.13
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_mariadb_vector-0.2.0.tar.gz.
File metadata
- Download URL: django_mariadb_vector-0.2.0.tar.gz
- Upload date:
- Size: 464.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
782a95d71c1e92de942bfca4c72b5bb6e876a5d9559fbd46d788d82a7461b676
|
|
| MD5 |
f2b04374c183f7f084a23923e9871a26
|
|
| BLAKE2b-256 |
c0fe30314fe3db68c7b8f9fa901d03a1092a0b4870db495136dd5b87f4459529
|
File details
Details for the file django_mariadb_vector-0.2.0-py3-none-any.whl.
File metadata
- Download URL: django_mariadb_vector-0.2.0-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4d5edc7ebd8dc7d1b6472c5af0ffc9735686d15befc060f3df3b952267535746
|
|
| MD5 |
0d83473b94d3343195e55a240556081c
|
|
| BLAKE2b-256 |
28618381528f708da28b786ab63ec25c21e0390e26743f245895878b330967e2
|