Skip to main content

A simple Django Field for storing auto-incrementing field.

Project description


Installation

Install django-composite-auto-field:

pip install django-composite-auto-field

Usage

This field uses Django’s Aggregate and Max functions to extract the latest code from the database and does a minimum of parsing to calculate the next code. This avoids having to store a counter in the database. Therefore, for correct operation, once the field arguments have been set, it is advisable not to modify them anymore, or to manually act on the codes already calculated and stored in the database.

Import to your models:

from django_composite_auto_field.fields import CompositeAutoField

Usage:

class Order(models.Model):
    code = CompositeAutoField(prefix='ORD', use_year=True, zeros=5)

Arguments:

prefix      # It's used to indicate prefix for the code, default is "CC"
use_year    # When it's True, last two numbers of current year will be used after prefix
zero        # Indicated number of zeros before the number

Run:

python manage.py makemigrations
python manage.py migrate

For example. Using Arguments from above as shown, will result in codes:

ORD2100001
ORD2100002
ORD2100003
...

If the year is used, every year the counter will be automatically reset and the count will restart with 1. If you pass a string to the field, it will be used instead of the generated value. Pay attention to the passed values, they will be used for the generation of subsequent values.

Tests

To run tests:

# clone this repository on your PC

# create the virtual environment
python -m venv venv

# activate the virtual environment
source venv\bin\activate

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

# run migrations
python manage.py migrate

# run tests
python manage.py test

Release Notes

  • 0.1.5
    • fixed code generation with prefix length different from 2

  • 0.1.4
    • fixed setup

    • fixed description file (README.rst)

  • 0.1.3
    • added tests

    • added support for custom string as argument

    • added support for Django 4.2 on python 3.8, 3.9, 3.10 and 3.11 - as per the official django docs

  • 0.1.2
    • fixed year change

  • 0.1.1 - initial release
    • provides CompositeAutoField for storing auto-incrementing field

    • supports Django 3.1 on python 3.6, 3.7, 3.8 and 3.9 - as per the official django docs

Contributing

It’s an open source project, so any contributions are welcome!

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-composite-auto-field-0.1.5.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file django-composite-auto-field-0.1.5.tar.gz.

File metadata

File hashes

Hashes for django-composite-auto-field-0.1.5.tar.gz
Algorithm Hash digest
SHA256 cd7e7c3aeb3755b520e81dbeb9ff1c814332324f10aae0943f7c13dd8ee7a396
MD5 a102c52fcf52b0f26320ca8726ad2b50
BLAKE2b-256 0c8fa3d91f94c36bfc281b2f093f8ca161f761d0c555e902cdb54c80d7269096

See more details on using hashes here.

File details

Details for the file django_composite_auto_field-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for django_composite_auto_field-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 292d2e383022645a49253122aefa6ca98989279b847383e9ba18e567c25f9338
MD5 0aad9f879c4813e1c80c9884ed4cf99f
BLAKE2b-256 82c0bd3c804e6ed01c0b07bee812f355a842abb062e58d67b861453d595be6f1

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