Name | django-sequencefield JSON |
Version |
1.0.13
JSON |
| download |
home_page | None |
Summary | 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 |
upload_time | 2024-10-04 09:55:55 |
maintainer | None |
docs_url | None |
author | None |
requires_python | None |
license | MIT License Copyright (c) 2024-present Loic Quertenmont Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
keywords |
django
postgresql
sequence
field
autofield
python
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
[![](https://img.shields.io/pypi/pyversions/django-sequencefield.svg?color=3776AB&logo=python&logoColor=white)](https://www.python.org/)
[![](https://img.shields.io/pypi/djversions/django-sequencefield?color=0C4B33&logo=django&logoColor=white&label=django)](https://www.djangoproject.com/)
[![Published on Django Packages](https://img.shields.io/badge/Published%20on-Django%20Packages-0c3c26)](https://djangopackages.org/packages/p/django-sequencefield/)
[![](https://img.shields.io/pypi/v/django-sequencefield.svg?color=blue&logo=pypi&logoColor=white)](https://pypi.org/project/django-sequencefield/)
[![](https://static.pepy.tech/badge/django-sequencefield/month)](https://pepy.tech/project/django-sequencefield)
[![](https://img.shields.io/github/stars/quertenmont/django-sequencefield?logo=github&style=flat)](https://github.com/quertenmont/django-sequencefield/stargazers)
[![](https://img.shields.io/pypi/l/django-sequencefield.svg?color=blue)](https://github.com/quertenmont/django-sequencefield/blob/main/LICENSE.txt)
[![](https://results.pre-commit.ci/badge/github/quertenmont/django-sequencefield/main.svg)](https://results.pre-commit.ci/latest/github/quertenmont/django-sequencefield/main)
[![](https://img.shields.io/github/actions/workflow/status/quertenmont/django-sequencefield/test-package.yml?branch=main&label=build&logo=github)](https://github.com/quertenmont/django-sequencefield)
[![](https://img.shields.io/codecov/c/gh/quertenmont/django-sequencefield?logo=codecov)](https://codecov.io/gh/quertenmont/django-sequencefield)
[![](https://img.shields.io/codacy/grade/194566618f424a819ce43450ea0af081?logo=codacy)](https://www.codacy.com/app/quertenmont/django-sequencefield)
[![](https://img.shields.io/badge/code%20style-black-000000.svg?logo=python&logoColor=black)](https://github.com/psf/black)
[![](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
# 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 sequence-field`
- 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:
```python
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](https://www.postgresql.org/docs/current/functions-formatting.html)). In the example bellow, we will get sequence values: INV_000001, INV_000002, INV_000003, ...
```python
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:
```python
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
```bash
# 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](LICENSE.txt).
---
## Supporting
- :star: Star this project on [GitHub](https://github.com/quertenmont/django-sequencefield)
- :octocat: Follow me on [GitHub](https://github.com/quertenmont)
- :blue_heart: Follow me on [Twitter](https://twitter.com/LoicQuertenmont)
- :moneybag: Sponsor me on [Github](https://github.com/sponsors/quertenmont)
Raw data
{
"_id": null,
"home_page": null,
"name": "django-sequencefield",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": "Loic Quertenmont <loic@deeperanalytics.be>",
"keywords": "django, postgresql, sequence, field, autofield, python",
"author": null,
"author_email": "Loic Quertenmont <loic@deeperanalytics.be>",
"download_url": "https://files.pythonhosted.org/packages/c0/04/428bc88c198f583015c252247f5f3b6d8970b77d90e1db7a01c565a05416/django_sequencefield-1.0.13.tar.gz",
"platform": null,
"description": "[![](https://img.shields.io/pypi/pyversions/django-sequencefield.svg?color=3776AB&logo=python&logoColor=white)](https://www.python.org/)\n[![](https://img.shields.io/pypi/djversions/django-sequencefield?color=0C4B33&logo=django&logoColor=white&label=django)](https://www.djangoproject.com/)\n[![Published on Django Packages](https://img.shields.io/badge/Published%20on-Django%20Packages-0c3c26)](https://djangopackages.org/packages/p/django-sequencefield/)\n\n[![](https://img.shields.io/pypi/v/django-sequencefield.svg?color=blue&logo=pypi&logoColor=white)](https://pypi.org/project/django-sequencefield/)\n[![](https://static.pepy.tech/badge/django-sequencefield/month)](https://pepy.tech/project/django-sequencefield)\n[![](https://img.shields.io/github/stars/quertenmont/django-sequencefield?logo=github&style=flat)](https://github.com/quertenmont/django-sequencefield/stargazers)\n[![](https://img.shields.io/pypi/l/django-sequencefield.svg?color=blue)](https://github.com/quertenmont/django-sequencefield/blob/main/LICENSE.txt)\n\n[![](https://results.pre-commit.ci/badge/github/quertenmont/django-sequencefield/main.svg)](https://results.pre-commit.ci/latest/github/quertenmont/django-sequencefield/main)\n[![](https://img.shields.io/github/actions/workflow/status/quertenmont/django-sequencefield/test-package.yml?branch=main&label=build&logo=github)](https://github.com/quertenmont/django-sequencefield)\n[![](https://img.shields.io/codecov/c/gh/quertenmont/django-sequencefield?logo=codecov)](https://codecov.io/gh/quertenmont/django-sequencefield)\n[![](https://img.shields.io/codacy/grade/194566618f424a819ce43450ea0af081?logo=codacy)](https://www.codacy.com/app/quertenmont/django-sequencefield)\n[![](https://img.shields.io/badge/code%20style-black-000000.svg?logo=python&logoColor=black)](https://github.com/psf/black)\n[![](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)\n\n\n\n# sequencefield\nsimple model field taking it's value from a postgres sequence. This is an easy replacement for django autofield offering the following advantages:\n- Sequence could be shared among multiple models (db tables), so you can now have unique id among multiple django models\n- Possibility to generate alphanumeric id of the form \"{PREFIX}_{ID}\"\n- 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)\n\n\n---\n\n## Installation\n- Run `pip install sequence-field`\n- Use a (Small/Big)IntegerSequenceField in one of your model\n- Add a SequenceConstraint into the same model to name the sequence field to use and which id should take values from this sequence constraint\n\n---\n\n## Usage\n\n### Settings\nThis package doesn't need any setting.\n\n### Simple Example\nJust add a sequence field(s) to your models like this:\n\n```python\nfrom django.db import models\nfrom sequencefield.constraints import IntSequenceConstraint\nfrom sequencefield.fields import IntegerSequenceField\n\n\nclass IntSequenceModel(models.Model):\n id = IntegerSequenceField(primary_key=True) #primary_key=False works too\n\n class Meta:\n constraints = [\n IntSequenceConstraint(\n name=\"%(app_label)s_%(class)s_custseq\",\n sequence=\"int_custseq\", #name of sequence to use\n drop=False, #avoid deleting the sequence if shared among multiple tables\n fields=[\"id\"], #name of the field that should be populated by this sequence\n start=100, #first value of the sequence\n 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\n )\n ]\n\n```\n\n### Simple AlphaNumeric Example\nJust add an AlphaNumericSequenceField field(s) to your models like this.\nYou 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](https://www.postgresql.org/docs/current/functions-formatting.html)). In the example bellow, we will get sequence values: INV_000001, INV_000002, INV_000003, ...\n\n```python\nfrom django.db import models\nfrom sequencefield.constraints import IntSequenceConstraint\nfrom sequencefield.fields import AlphaNumericSequenceField\n\nclass AlphaNumericSequenceModel(models.Model):\n seqid = AlphaNumericSequenceField(\n primary_key=False, prefix=\"INV\", format=\"FM000000\"\n )\n\n class Meta:\n constraints = [\n IntSequenceConstraint(\n name=\"%(app_label)s_%(class)s_custseq\",\n sequence=\"alphanum_custseq\", , #name of sequence to use\n drop=False, #avoid deleting the sequence if shared among multiple tables\n fields=[\"seqid\"], #name of the field that should be populated by this sequence\n start=1,\n )\n ]\n```\n\n### Advance Example\nJust add a sequence field(s) to your models like this:\n\n```python\nfrom django.db import models\nfrom sequencefield.constraints import BigIntSequenceConstraint\nfrom sequencefield.fields import BigIntegerWithDateSequenceField\n\n\nclass BigIntSequenceModel(models.Model):\n id = models.BigIntegerField(primary_key=True, auto_created=False)\n created = models.DateTimeField(editable=False)\n seqid = BigIntegerWithDateSequenceField(datetime_field=\"created\") #this field with combine values from the sequence with date timestamp\n # the 2 first bytes of the bigint will contains the number of days since 1/1/1970\n # the 6 following bytes will contains a unique id coming from the sequence\n\n class Meta:\n constraints = [\n BigIntSequenceConstraint(\n name=\"%(app_label)s_%(class)s_custseq\",\n sequence=\"gdw_post_custseq\", #name of the quence\n drop=False, #avoid deleting the sequence if shared among multiple tables\n fields=[\"seqid\"], #field to be populated from this sequence\n start=1, #first value of the sequence\n )\n ]\n```\n\n---\n\n## Remarks\n\nUntil we find a good solution to properly handle supression of a sequence shared among multiple tables,\nIt'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.\n\n---\n\n## Testing\n```bash\n# clone repository\ngit clone https://github.com/quertenmont/django-sequencefield.git && cd sequencefield\n\n# create virtualenv and activate it\npython -m venv venv && . venv/bin/activate\n\n# upgrade pip\npython -m pip install --upgrade pip\n\n# install requirements\npip install -r requirements.txt -r requirements-test.txt\n\n# install pre-commit to run formatters and linters\npre-commit install --install-hooks\n\n# run tests\ntox\n# or\npython runtests.py\n# or\npython -m django test --settings \"tests.settings\"\n```\n---\n\n## License\nReleased under [MIT License](LICENSE.txt).\n\n---\n\n## Supporting\n\n- :star: Star this project on [GitHub](https://github.com/quertenmont/django-sequencefield)\n- :octocat: Follow me on [GitHub](https://github.com/quertenmont)\n- :blue_heart: Follow me on [Twitter](https://twitter.com/LoicQuertenmont)\n- :moneybag: Sponsor me on [Github](https://github.com/sponsors/quertenmont)\n",
"bugtrack_url": null,
"license": "MIT License Copyright (c) 2024-present Loic Quertenmont Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
"summary": "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",
"version": "1.0.13",
"project_urls": {
"Documentation": "https://github.com/quertenmont/django-sequencefield#readme",
"Download": "https://github.com/quertenmont/django-sequencefield/releases",
"Funding": "https://github.com/sponsors/quertenmont/",
"Homepage": "https://github.com/quertenmont/django-sequencefield",
"Issues": "https://github.com/quertenmont/django-sequencefield/issues",
"Twitter": "https://twitter.com/LoicQuertenmont"
},
"split_keywords": [
"django",
" postgresql",
" sequence",
" field",
" autofield",
" python"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "324711658478e543c6b7ffe5b8f96fe2f959dffb75532277a4fd4cb93e461421",
"md5": "016ff649d528b820a3f95f4c0709306d",
"sha256": "663ecdec32738a453764c81fa9e0dca22c1b03dba04ff2e3453bd5487a471671"
},
"downloads": -1,
"filename": "django_sequencefield-1.0.13-py3-none-any.whl",
"has_sig": false,
"md5_digest": "016ff649d528b820a3f95f4c0709306d",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 9373,
"upload_time": "2024-10-04T09:55:53",
"upload_time_iso_8601": "2024-10-04T09:55:53.291239Z",
"url": "https://files.pythonhosted.org/packages/32/47/11658478e543c6b7ffe5b8f96fe2f959dffb75532277a4fd4cb93e461421/django_sequencefield-1.0.13-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "c004428bc88c198f583015c252247f5f3b6d8970b77d90e1db7a01c565a05416",
"md5": "43bc37bcdfb98ad85e02843abca0a2a6",
"sha256": "399d8609416bf48ad0f9c37d71053a0c8994f484b78de0ae5296daf1457ee890"
},
"downloads": -1,
"filename": "django_sequencefield-1.0.13.tar.gz",
"has_sig": false,
"md5_digest": "43bc37bcdfb98ad85e02843abca0a2a6",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 8862,
"upload_time": "2024-10-04T09:55:55",
"upload_time_iso_8601": "2024-10-04T09:55:55.034865Z",
"url": "https://files.pythonhosted.org/packages/c0/04/428bc88c198f583015c252247f5f3b6d8970b77d90e1db7a01c565a05416/django_sequencefield-1.0.13.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-04 09:55:55",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "quertenmont",
"github_project": "django-sequencefield#readme",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"tox": true,
"lcname": "django-sequencefield"
}