Simple database sharding (horizontal partitioning) library for Django applications.
Project description
Django Horizon
Purpose
Simple database sharding (horizontal partitioning) library for Django applications.
Free software: MIT license
Documentation: https://django-horizon.readthedocs.io.
Inspired by django-sharding. Thank you so much for your cool solution :)
Features
Shard (horizontal partitioning) by some ForeignKey field like user account.
Installation
To install Django Horizon, run this command in your terminal:
$ pip install django-horizon
This is the preferred method to install Django Horizon, as it will always install the most recent stable release.
If you don’t have pip installed, this Python installation guide can guide you through the process.
Usage
Setup
Add database router configuration in your settings.py:
Horizontal database groups and a metadata store
HORIZONTAL_CONFIG = {
'GROUPS': {
'group1': { # The name of database horizontal partitioning group
'DATABASES': {
1: {
'write': 'member1-primary',
'read': ['member1-replica-1', 'member1-replica-2'], # Pick randomly by router
},
2: {
'write': 'member2-primary',
'read': ['member2-replica'],
},
3: {
'write': 'a3', # Used by 'read' too
},
},
'PICKABLES': [2, 3], # Group member keys to pick new database
},
},
'METADATA_MODEL': 'app.HorizontalMetadata', # Metadata store for horizontal partition key and there database
}
Database router
DATABASE_ROUTERS = (
'horizon.routers.HorizontalRouter',
...
)
Example models
Horizontal partitioning by user
Metadata store
from horizon.models import AbstractHorizontalMetadata
class HorizontalMetadata(AbstractHorizontalMetadata):
pass
In the example, metadata store save followings.
group: Group name for horizontal partitioning.
key: Determines the distribution of the table’s records among the horizontal partitioning group.
index: Choosed database index in horizontal partitioning groups.
Using a model
from django.contrib.auth import get_user_model
user_model = get_user_model()
user = user_model.objects.get(pk=1)
# Get by foreign instance
SomeLargeModel.objects.filter(uses=user)
# Get by foreign id
SomeLargeModel.objects.filter(uses_id=user.id)
Model limitations
django.db.utils.IntegrityError occured when not specify horizontal key field to filter
SomeLargeModel.objects.all()
Cannot lookup by foreign key field, cause there are other (like default) database
list(user.somelargemodel_set.all())
History
1.1.2 (2018-11-15)
Update _create_object_from_params to new interface
Add support for Django 2.1
Add support for Python 3.7
1.1.1 (2018-08-03)
Migrate to QuerySet as a mixin
1.1.0 (2018-03-30)
Drop support for Django 1.9, 1.10
1.0.0 (2018-02-02)
Add support for Django 2.0
Drop support for Django 1.8
0.0.1 (2017-05-22)
First release on PyPI.
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
File details
Details for the file django-horizon-1.1.2.tar.gz
.
File metadata
- Download URL: django-horizon-1.1.2.tar.gz
- Upload date:
- Size: 239.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f29e9052438764f76e629d88cb7257f36ba40dc62681b42532ecf998fd5bce8 |
|
MD5 | 56ff245b6ebba7f220a1e888b512d2d3 |
|
BLAKE2b-256 | a78a103c4ac0eb0143fe89cadcf0c117e3116090785f62b150de8de0e09fc752 |
File details
Details for the file django_horizon-1.1.2-py2.py3-none-any.whl
.
File metadata
- Download URL: django_horizon-1.1.2-py2.py3-none-any.whl
- Upload date:
- Size: 10.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b3a2d266c49a5fd0b8d87c130835c072d1adeb3a7f8c0155ab70ae9a9a254edc |
|
MD5 | a166ac399d8f922f5115018416621ee2 |
|
BLAKE2b-256 | fe1f5a1020a856f87374d130c2c81ac3ae7816c2e222df0ce96e2b00999704eb |