Skip to main content

Django Horizon

https://travis-ci.org/uncovertruth/django-horizon.svg?branch=master https://codecov.io/gh/uncovertruth/django-horizon/branch/master/graph/badge.svg Documentation Status Updates Python 3 https://img.shields.io/pypi/v/django-horizon.svg

Purpose

Simple database sharding (horizontal partitioning) library for Django applications.

Logo

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 keep user’s pk and that index of horizontal database (1, 2 or 3).

Sharded model

from django.conf import settings

from horizon.manager import HorizontalManager  # For Django<1.10
from horizon.models import AbstractHorizontalModel


class SomeLargeModel(AbstractHorizontalModel):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    ...

    objects = HorizontalManager()  # For Django<1.10

    class Meta(object):
        horizontal_group = 'group1'  # Group name
        horizontal_key = 'user'  # Group key

In many cases use UUIDField field for id. The AbstractHorizontalModel uses UUIDField as a them id field in default.

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)

# django.db.utils.IntegrityError occured when not specify horizontal key field to filter
SomeLargeModel.objects.all()

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(self.user.somelargemodel_set.all())

History

0.0.1 (2017-05-20)

  • First release on PyPI.

Metadata

Release files for django-horizon 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-horizon 0.0.1
File Size Uploaded Provenance Actions
django-horizon-0.0.1.tar.gz 239.7 kB Without provenance Details for django-horizon-0.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-horizon 0.0.1
File Interpreter ABI Platform Actions
django_horizon-0.0.1-py2.py3-none-any.whl Python 2, Python 3 none any Details for django_horizon-0.0.1-py2.py3-none-any.whl

Total release size: 251.3 kB

Release files / django-horizon-0.0.1.tar.gz

Download URL django-horizon-0.0.1.tar.gz
Size 239.7 kB
Tags Source
SHA-256 checksum
How to use checksums
05b1bb5efeabecadce3560e172bb847f6ef8d1c06ad7a3830f77a4838eee4fd9
BLAKE2b-256 checksum
How to use checksums
4309a4432b2c54112b11f7b68b9716442c450aea339b6f963d7e94c0ed0621d5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_horizon-0.0.1-py2.py3-none-any.whl

Download URL django_horizon-0.0.1-py2.py3-none-any.whl
Size 11.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
795f1a713650b423d1d94d720dae69f5eac23a86cd2d93886990cbf6f306c2d3
BLAKE2b-256 checksum
How to use checksums
e64fc3918bcb1c226da0b858d681a8ebd3c5feea5dbdefacf99647b1410174af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for django-horizon 0.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page