Skip to main content

Published on Django Packages

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 django-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). In the example bellow, we will get sequence values: INV_000001, INV_000002, INV_000003, ...

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

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"

Dependabot auto-merge

  • Dependabot PRs automatically get auto-merge enabled by .github/workflows/dependabot-auto-merge.yml.
  • The workflow only runs for Dependabot-authored, non-draft PR updates (github.actor == 'dependabot[bot]').
  • It uses the repository GITHUB_TOKEN and enables squash auto-merge.
  • Prerequisites:
    • Repository setting Allow auto-merge must be enabled.
    • Branch protection must define required status checks; merge happens only after those checks pass.

License

Released under MIT License.


Supporting

Metadata

Release files for django-sequencefield 1.0.17

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-sequencefield 1.0.17
File Size Uploaded Provenance Actions
django_sequencefield-1.0.17.tar.gz 9.3 kB With provenance Details for django_sequencefield-1.0.17.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-sequencefield 1.0.17
File Interpreter ABI Platform Actions
django_sequencefield-1.0.17-py3-none-any.whl Python 3 none any Details for django_sequencefield-1.0.17-py3-none-any.whl

Total release size: 19.0 kB

Release files / django_sequencefield-1.0.17.tar.gz

Download URL django_sequencefield-1.0.17.tar.gz
Size 9.3 kB
Tags Source
SHA-256 checksum
How to use checksums
edc8ba9b42167b6b1d6942ddc484c592396a724b9af596ee3128739ee9da1d69
BLAKE2b-256 checksum
How to use checksums
b16d7c4fb5e5a41c3a687a6b99c21451f3a64efc9ec7228140372e565710c7e8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 2, 2026.

Transparency log

Release files / django_sequencefield-1.0.17-py3-none-any.whl

Download URL django_sequencefield-1.0.17-py3-none-any.whl
Size 9.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8c95f6f79e937aa9b5fda63fa2197025f34c9ca8f8981be4f205ddffe1a9a925
BLAKE2b-256 checksum
How to use checksums
df034303f1fb898553a5b024f472029ed46c249d311e7f97be180de4494d0c28
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 2, 2026.

Transparency log

Security for django-sequencefield 1.0.17

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.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for django_sequencefield-1.0.17.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 2, 2026.

Transparency log

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