Skip to main content
# django-multi-db-relation

> Queryset optimization supports for multi database spanning relations between Django models.

## Requirements

- Python (>= 3.5)
- Django (>= 1.10)

## Installation

```sh
pip install django-multi-db-relation
```

## Usage

For models

```python
class ModelA(models.Model):
name = models.CharField(max_length=10)

class ModelB(models.Model):
a = models.ForeignKey(ModelA, on_delete=models.DO_NOTHING)

class ModelC(models.Model):
b = models.ForeignKey(ModelB, on_delete=models.DO_NOTHING, db_constraint=False)
```

suppose that `ModelA` and `ModelB` is routed to `db1` and `ModelC` to `db2`.

We **cannot** run a queryset with `select_related()`.

```python
>>> ModelC.objects.select_related('b') # Table not found
```

In this case, modify `ModelC` like:

```python
from multi_db_relation.mixins import ExternalDbQuerySetMixin


class ModelCQuerySet(ExternalDbQuerySetMixin, models.QuerySet):
pass

class ModelC(models.Model):
b = models.ForeignKey(ModelB, on_delete=models.DO_NOTHING, db_constraint=False)

objects = models.Manager.from_queryset(queryset_class=ModelCQuerySet)()

class Meta:
external_db_fields = ['b']
```

then:

```python
>>> ModelC.objects.select_related('b') # Number of queries is optimized from O(n) to O(1)
>>> ModelC.objects.select_related('b__a') # Also works well
```

## License

- See [LICENSE](LICENSE)

Release files for django-multi-db-relation 0.1

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

Built distribution (wheel)

Table of built distributions (wheels) for django-multi-db-relation 0.1
File Interpreter ABI Platform
django_multi_db_relation-0.1-py3-none-any.whl Python 3 none any Details

Release files / django_multi_db_relation-0.1-py3-none-any.whl

Download URL django_multi_db_relation-0.1-py3-none-any.whl
Size 4.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
289f9535f6e0fd9e52a8f0f4ba24f954696031ca7f9e07cedafd5be50ae63d7e
BLAKE2b-256 checksum
How to use checksums
313c77c0dcee5d9bcf7bd66a894eff421dd1b5862f8a7ecac35f42b633383636
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1 This release

1 release file

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