Skip to main content

Additional field from django taking it's value from a postgresql sequence. It is similar to django AutoField, except that multiple model can share ids from a single sequence. Also add the possibility to generate an id from a sequence AND from the data of another field in the model

Project description

sequencefield

simple model field taking it's value from a postgres sequence. This is an easy replacement for django autofield offering the following advantages:

  • Sequence could be shared among multiple models (db tables), so you can now have unique id among multiple django models
  • Possibility to generate alphanumeric id of the form "{PREFIX}_{ID}"
  • Unique Id could also be combined with data from other field to build complex id. One example is given that combine unique id with date information to offer an efficient table indexing/clustering for faster data retrieval when filtering on date. (Particularly useful with BRIN index)

Installation

  • Run pip install sequencefield
  • Use a (Small/Big)IntegerSequenceField in one of your model
  • Add a SequenceConstraint into the same model to name the sequence field to use and which id should take values from this sequence constraint

Usage

Settings

This package doesn't need any setting.

Simple Example

Just add a sequence field(s) to your models like this:

from django.db import models
from sequencefield.constraints import IntSequenceConstraint
from sequencefield.fields import IntegerSequenceField


class IntSequenceModel(models.Model):
    id = IntegerSequenceField(primary_key=True) #primary_key=False works too

    class Meta:
        constraints = [
            IntSequenceConstraint(
                name="%(app_label)s_%(class)s_custseq",
                sequence="int_custseq", #name of sequence to use
                drop=False, #avoid deleting the sequence if shared among multiple tables
                fields=["id"], #name of the field that should be populated by this sequence
                start=100, #first value of the sequence
                maxvalue=200 #max value allowed for the sequence, will raise error if we go above, use None for the maximum allowed value of the db
            )
        ]

Simple AlphaNumeric Example

Just add an AlphaNumericSequenceField field(s) to your models like this. You can provide a "format" argument to define how to convert the number to char. The syntax is the one used in postgres to_char function (see here)

class AlphaNumericSequenceModel(models.Model): seqid = AlphaNumericSequenceField( primary_key=False, prefix="INV", format="FM000000" )

class Meta:
    constraints = [
        IntSequenceConstraint(
            name="%(app_label)s_%(class)s_custseq",
            sequence="alphanum_custseq", , #name of sequence to use
            drop=False, #avoid deleting the sequence if shared among multiple tables
            fields=["seqid"], #name of the field that should be populated by this sequence
            start=1,
        )
    ]

Advance Example

Just add a sequence field(s) to your models like this:

from django.db import models
from sequencefield.constraints import BigIntSequenceConstraint
from sequencefield.fields import BigIntegerWithDateSequenceField


class BigIntSequenceModel(models.Model):
    id = models.BigIntegerField(primary_key=True, auto_created=False)
    created = models.DateTimeField(editable=False)
    seqid = BigIntegerWithDateSequenceField(datetime_field="created") #this field with combine values from the sequence with date timestamp
    # the 2 first bytes of the bigint will contains the number of days since 1/1/1970
    # the 6 following bytes will contains a unique id coming from the sequence

    class Meta:
        constraints = [
            BigIntSequenceConstraint(
                name="%(app_label)s_%(class)s_custseq",
                sequence="gdw_post_custseq", #name of the quence
                drop=False, #avoid deleting the sequence if shared among multiple tables
                fields=["seqid"], #field to be populated from this sequence
                start=1, #first value of the sequence
            )
        ]

Remarks

Until we find a good solution to properly handle supression of a sequence shared among multiple tables, It's better to pass the flag drop=False in the SequenceConstraint. Otherwise a sequence that is still being used by another table might be deleted.


Testing

# clone repository
git clone https://github.com/quertenmont/django-sequencefield.git && cd sequencefield

# create virtualenv and activate it
python -m venv venv && . venv/bin/activate

# upgrade pip
python -m pip install --upgrade pip

# install requirements
pip install -r requirements.txt -r requirements-test.txt

# install pre-commit to run formatters and linters
pre-commit install --install-hooks

# run tests
tox
# or
python runtests.py
# or
python -m django test --settings "tests.settings"

License

Released under MIT License.


Supporting

  • :star: Star this project on GitHub
  • :octocat: Follow me on GitHub
  • :blue_heart: Follow me on Twitter
  • :moneybag: Sponsor me on Github

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_sequencefield-1.0.7.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

django_sequencefield-1.0.7-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file django_sequencefield-1.0.7.tar.gz.

File metadata

  • Download URL: django_sequencefield-1.0.7.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for django_sequencefield-1.0.7.tar.gz
Algorithm Hash digest
SHA256 b5b691ad566124ed18d44e0de0f54588b3a92c4c2205f41517da902fdfe3f00d
MD5 b8b2650ee00c348cdd70b59ae58981c7
BLAKE2b-256 1fb14dbc4734488634b24b8e6473af61cb9430c491cd7f637846829cbfe7d1b4

See more details on using hashes here.

File details

Details for the file django_sequencefield-1.0.7-py3-none-any.whl.

File metadata

File hashes

Hashes for django_sequencefield-1.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 d4d8b3cbdcdee422c2832eb809795250c742af893082aafc36203d99c1b018c7
MD5 48e4d1a8f08e3b9a1383a4a12c52e45d
BLAKE2b-256 a25b8573703469e267727fe3f01f8ab8b422623000242da1d82a6af3f39a6a48

See more details on using hashes here.

Supported by

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