Skip to main content

django-join-unrelated

Python version Tests

Use SQL Join with Django ORM models having no relations.

If you have models in your project that share the same data but have no relations, you might face a situation when you need to join them. But Django does not provide this functionality yet.

With django-join-unrelated you can do the following (models defined here):

from app.core.models import Jedi, Person

Person.objects.create(first_name='Padme', last_name='Amidala', birth_place='Naboo')
Person.objects.create(first_name='Obi-Wan', last_name='Kenobi', birth_place='Stewjon')
Jedi.objects.create(first_name='Obi-Wan', last_name='Kenobi', force=100)
Jedi.objects.create(first_name='Mace', last_name='Windu', force=80)

Jedi.objects.join(first_name=Person.first_name).values_list('first_name')
# <UnrelatedJoinQuerySet [('Obi-Wan',)]>

print(Jedi.objects.join(first_name=Person.first_name).values_list('first_name').query)
# SELECT "core_jedi"."first_name" FROM "core_jedi" INNER JOIN core_person ON ("core_jedi"."first_name" = core_person."first_name")

# Set join type explicitly
from django_join_unrelated import JoinType

Jedi.objects.join(first_name=Person.first_name, join_type=JoinType.FULL)

# Join on multiple fields
Jedi.objects.join(first_name=Person.first_name, last_name=Person.last_name)

# Join multiple models
Jedi.objects.join(first_name=Person.first_name, id=User.id)

django-join-unrelated tries to keep all QuerySet power where it is possible:

Jedi.objects.join(first_name=Person.first_name).filter(last_name='Kenobi')
# <UnrelatedJoinQuerySet [<Jedi: Obi-Wan Kenobi>]>

Jedi.objects.join(first_name=Person.first_name).annotate(name=F('first_name'))[0].name
# 'Obi-Wan'

Jedi.objects.join(first_name=Person.first_name).aggregate(Sum('force'))
# {'force__sum': 100}

Installation

You can install django-join-unrelated using pip:

pip install django-join-unrelated

Usage

Just import UnrelatedJoinManager and set it as an attribute to your models:

from django.db import models
from django_join_unrelated import UnrelatedJoinManager

class Jedi(models.Model):
    first_name = models.CharField('First name', max_length=128)
    last_name = models.CharField('Last name', max_length=128)
    force = models.IntegerField('Force')

    objects = UnrelatedJoinManager()

    class Meta:
        verbose_name = 'Jedi'

Already implemented:

  • filtering via joins

Not implemented yet but planned:

  • unrelated objects selection
  • return of joined values

Metadata

Release files for django-join-unrelated 0.2.0

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-join-unrelated 0.2.0
File Size Uploaded Provenance
django-join-unrelated-0.2.0.tar.gz 5.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for django-join-unrelated 0.2.0
File Interpreter ABI Platform
django_join_unrelated-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.0 kB

Release files / django-join-unrelated-0.2.0.tar.gz

Download URL django-join-unrelated-0.2.0.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
434b611d9fa1163711a47861c289de7c6c5f27aa1299d52ef94111085a75c710
BLAKE2b-256 checksum
How to use checksums
e771a570880ea4161adc360447a9523fb1a7d91371aaa3c593dfa159d2d21a1a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.8.15 Linux/5.19.0-43-generic

Release files / django_join_unrelated-0.2.0-py3-none-any.whl

Download URL django_join_unrelated-0.2.0-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fe0b23e246ebdb700eae03b8f22c83da877ea501fcb205ba5fc06b09b2448b85
BLAKE2b-256 checksum
How to use checksums
8064589a8b5e3caed840ad723c1699279088817915229ec00aa5af571f917520
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.8.15 Linux/5.19.0-43-generic

Security for django-join-unrelated 0.2.0

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.

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