Skip to main content

Extended Django Model class with composite-primary-key support

Project description

django-compositepk-model

Provide an extended Django Model class named 'CPkModel' that supports composite primary keys. Also provide an extended Query class 'CPkQuery' that supports multi-column lookups.

This package supports treating legacy db tables with a composite primary key, without adding a surrogate key.

This is my other approach to ticket373.

Features

1. Easy to use

For a table with a composite primary key, change the base class from Model to CPkModel, and set 'unique_together' in Meta. In addition, set 'primary_key=True' to each primary keys.

(model definition sample)

from django.db import models
from cpkmodel import CPkModel

# Normal Model
#   primary_key is auto 'id'
class Company(models.Model):
    name = models.CharField(max_length=100)
    established_date = models.DateField()
    company_code = models.CharField(max_length=100)

    class Meta:
        db_table = 'Company'

# Child Model (CpkModel)
#   composite primary key: company_id, country_code
class CompanyBranch(CPkModel):
    company = models.ForeignKey(
        Company,
        primary_key=True,       # for CompositePK
        on_delete=models.CASCADE)
    country_code = models.CharField(
        max_length=100,
        primary_key=True,       # for CompositePK
    )
    name = models.CharField(max_length=100)
    established_date = models.DateField()

    class Meta:
        db_table = 'CompanyBranch'
        unique_together = (('company', 'country_code'),)  # for CompositePK

That's all. No additional definitions or virtual fields are required.

2. Admin avairable

CPkModel can be used in Django Admin. The values of composite primary key are displayed in a comma separated style. Change(Update), Delete are fine. Add(Create) has a problem that CreateView do unique check to each key Field. So you can't add enough child records. But, this is only CreateView's problem. Your program can create child records by QuerySet or Model method.

3. Multi-column lookup avairable

To support composite primary keys, CPkQuery supports multi-column lookups.

obj = CompanyBranch.objects.get(pk=(1,'JP'))
qs = CompanyBranch.objects.filter(pk='1,JP')
qs = CompanyBranch.objects.filter(**{'company,country_code':(1,'JP')})
qs = CompanyBranch.objects.filter(**{'company_id,country_code':'1,JP'})

LHS with comma is not just primary, also other columns are OK.

qs = CompanyBranch.objects.filter(**{'country_code,name':'JP,Google'})

Lookup Multi-Column with 'in' is also avairable. PostgreSQL is fine, But SQLite3 is not supported.

qs = CompanyBranch.objects.filter(pk__in=[(1,'JP'),(1,'US'),(2,'JP'),])
qs = CompanyBranch.objects.filter(**{'country_code,name__in':[('JP','HONDA'),('CN','SONY'),]})

4. bulk_update avairable (v1.0.2)

bulk_update methond avairable for PostgreSQL. But SQLite3 is not supported.

   Album.objects.bulk_update(albums, ['num_stars',])

Installation

pip install django-compositepk-model

Links

https://code.djangoproject.com/ticket/373
https://code.djangoproject.com/wiki/MultipleColumnPrimaryKeys
https://gijutsu.com/en/2021/01/19/django-composite-primary-key/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-compositepk-model-1.0.2.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

django_compositepk_model-1.0.2-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file django-compositepk-model-1.0.2.tar.gz.

File metadata

  • Download URL: django-compositepk-model-1.0.2.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.7.8

File hashes

Hashes for django-compositepk-model-1.0.2.tar.gz
Algorithm Hash digest
SHA256 93e4979937a5dc05c08fe7f61bdb6d84e27d8553f5edbb7ded4c2479e4709408
MD5 b1338be8cb42c1853fffaf9e984dd72c
BLAKE2b-256 739d3d01605daf7df6bf5d27e340a189d811d7497cb3e98c5301ae9699c8c3c7

See more details on using hashes here.

File details

Details for the file django_compositepk_model-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: django_compositepk_model-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.7.8

File hashes

Hashes for django_compositepk_model-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 69f06104d04bc452caec3132fcd94df46d99af33793e8398b0dd6751898bc608
MD5 5e7711d148849cac6037ceb7ffde0fa5
BLAKE2b-256 dafedcc7c228803ee0c576229aecfe7eaee619e0841e728ae071c503718691a9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page