Giter Club home page Giter Club logo

django-sequences's Introduction

django-sequences

The problem

Django's default, implicit primary keys aren't guaranteed to be sequential.

If a transaction inserts a row and then is rolled back, the sequence counter isn't rolled back for performance reasons, creating a gap in primary keys.

This can cause compliance issues for some use cases such as accounting.

This risk isn't well known. Since most transactions succeed, values look sequential. Gaps will only be revealed by audits.

The solution

django-sequences provides just two function, get_next_value and get_next_n_value, which is designed to be used as follows:

from django.db import transaction

from sequences import get_next_value

from invoices.models import Invoice

with transaction.atomic():
    Invoice.objects.create(number=get_next_value('invoice_numbers'))

The guarantees of django-sequences only apply if you call get_next_value and save its return value to the database within the same transaction!

Installation

Install django-sequences:

cat requirements.txt
--trusted-host=pypi.luojilab.com
--index-url http://pypi.luojilab.com/rock2018/pypi

django-sequences=0.1.5    # 请安装最新版本
pip install -r requirements.txt
$ pip install django-sequences

Add it to the list of applications in your project's settings:

INSTALLED_APPS = [
'...',
'sequences',

]

Run migrations:

$ ./manage.py migrate

API

get_next_value generates a gap-less sequence of integer values:

>>> get_next_value()
1
>>> get_next_value()
2
>>> get_next_value()
3

It supports multiple independent sequences:

>>> get_next_value('PO')
1
>>> get_next_value('PO')
2
>>> get_next_value('PO')
1
>>> get_next_value('PO')
2

It supports multiple values sequences:

>>> get_next_n_value(5, 'PO')
[0, 1, 2, 3, 4]
>>> get_next_value(5, 'PO')
[5, 6, 7, 8, 9]
>>> get_next_n_value(5, 'SO')
[0, 1, 2, 3, 4]
>>> get_next_value(5, 'SO')
[5, 6, 7, 8, 9]

django-sequences's People

Contributors

aaugustin avatar hvdklauw avatar rmoch avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.