django-charid-field


Namedjango-charid-field JSON
Version 0.4 PyPI version JSON
download
home_pagehttps://github.com/yunojuno/django-charid-field
SummaryProvides a char-based, prefixable ID field for your Django models. Supports cuid, ksuid, ulid, et al.
upload_time2023-11-15 11:17:43
maintainerYunoJuno
docs_urlNone
authorYunoJuno
requires_python>=3.8,<4.0
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # django-charid-field

[![PyPI version](https://badge.fury.io/py/django-charid-field.svg)](https://badge.fury.io/py/django-charid-field)

Provides a char-based, prefixable CharIDField for your Django models.

It can utilise [cuid], [ksuid], [ulid] or any other string-based UID generation systems.

It can be used as the primary key, or simple another key on your models.

[cuid]: https://github.com/ericelliott/cuid
[ksuid]: https://github.com/segmentio/ksuid
[ulid]: https://github.com/ulid/spec

## โ›ฒ Feature set

-   Ability to work with the UID generation spec of your choice.
-   Support for prefixing the ID on a per-model basis ร  la Stripe. e.g `cus_` => `cus_cjld2cjxh0000qzrmn831i7rn`
-   Support for all database backends that support the `CharField`.
-   Support for Python 3.8 & above only.

## ๐Ÿคท Why?

To get us a global namespace of collision-resistant IDs that:

* are URL-safe
* can be represented in a visual-space-efficient manor
* are collision-resistant to allow for client side generation
* exist now. UUID v6, v7, v8 are in RFC draft and not ready (Jul '21).

[cuid], [ksuid], [ulid] & many others offer this now, and prefixing gets us the global namespace.

**Why not use integers?**

* Auto-incrementing integers are easily enumerable and give away collection count.

* You can solve that with HashID but then you either have to store the HashID as another column or deal with constant conversion when looking up values in your UI VS raw in your database.

* Most importantly: relying on your database to generate IDs means sequential writes. Your clients are not free to generate their own IDs without a round trip to the database.

**Why not use UUIDs?**

They solve the collision problem so why not?

* The text formats use hex, which is not visually space-efficient.
* UUIDv4 (the one usually recommended) is completely random and thus impossible to sort. This has the knock on effect of making databases work harder when looking up/indexing as binary search goes out the window.
* Optional hyphenation when representing the hex. This nuance results in more code.

**Why prefix?**

Because global flat namespaces are powerful. An ID now represents the instance _and it's type_, which means you can have powerful lookup abilities with just the identifier alone. No more guessing whether `802302` is a `Dog` or a `Cat`.

## ๐Ÿ“— Install

Install using your favourite Python dependency manager, or straight with pip:

```
pip install django-charid-field
```

You'll also need to install your ID-generation library of choice (or bring your own).

For example:

|UID Spec|Python Library|What could it look like? (with a prefix `dev_`)|
|--------|--------------|----------------------------------------|
|[cuid]|cuid.py: [GH](https://github.com/necaris/cuid.py) / [PyPi](https://pypi.org/project/cuid/)|`dev_ckpffbliw000001mi3fw42vsn`|
|[ksuid]|cyksuid: [GH](https://github.com/timonwong/cyksuid) / [PyPi](https://pypi.org/project/cyksuid/)|`dev_1tOMP4onidzvnUFuTww2UeamY39`|
|[ulid]|python-ulid: [GH](https://github.com/mdomke/python-ulid) / [PyPi](https://pypi.org/project/python-ulid/)|`dev_01F769XGM83VR75H86ZPHKK595`|



## โœจ Usage

```
from charidfield import CharIDField
```

We recommend using `functool.partial` to create your own field for your codebase; this will allow you to specify your chosen ID generation and set the `max_length` parameter and then have an importable field you can use across all your models.

Here's an example using the cuid spec and cuid.py:

```python
# Locate this somewhere importable
from cuid import cuid
from charidfield import CharIDField

CuidField = partial(
    CharIDField,
    default=cuid,
    max_length=30,
    help_text="cuid-format identifier for this entity."
)

# models.py
from wherever_you_put_it import CuidField

class Dog(models.Model):
    id = CuidField(primary_key=True, prefix="dog_")
    name = models.CharField()

# shell
>>> dog = Dog(name="Ronnie")
>>> dog.id
"dog_ckpffbliw000001mi3fw42vsn"

```

### Parameters

|Param|Type|Required|Default|Note|
|-----|----|--------|-------|----|
|**default**|`Callable`|โŒ|-|This should be a callable which generates a UID in whatever system you chose. Your callable does not have to handle prefixing, the prefix will be applied onto the front of whatever string your default callable generates. Technically not required, but without it you will get blank fields and must handle ID generation yourself.|
|**prefix**|`str` |โŒ|`""`|If provided, the ID strings generated as the field's default value will be prefixed. This provides a way to have a per-model prefix which can be helpful in providing a global namespace for your ID system. The prefix should be provided as a string literal (e.g `cus_`). For more, see below.|
|**max_length**|`int`|โœ…|Set it|Controls the maximum length of the stored strings. Provide your own to match whatever ID system you pick, remembering to take into account the length of any prefixes you have configured. Also note that there is no perf/storage impact for modern Postgres so for that backend it is effectively an arbitary char limit.|
|**primary_key**|`boolean`|โŒ|`False`|Set to `True` to replace Django's default `Autofield` that gets used as the primary key, else the field will be additional ID field available to the model.|
|**unique**|`boolean`|โŒ|`True`|Whether the field should be treated as unique across the dataset; the field provides a sane default of `True` so that a database index is setup to protext you against collisions (whether due to chance or, more likely, a bug/human error). To turn the index off, simply pass `False`.|

All other `django.db.models.fields.CharField` keyword arguments should work as expected. See the [Django docs](https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.CharField).

### Usage as the Primary Key


This will replace Django's `AutoField` and the cuid will become the main primary key
for the entity, thus removing the default database-genererated incremental integer ID.

```python
# models/some_model.py or models.py

class SomeModel(models.Model):
    id = CharIDField(primary_key=True, default=your_id_generator)

>>> some_model = SomeModel.objects.create()
>>> some_model.id
"ckp9jm3qn001001mrg5hw3sk4"
>>> some_model.pk
"ckp9jm3qn001001mrg5hw3sk4"
""
```
### Setting up prefixing

#### What?

Prefixing allows per-entity ID namespacing, e.g:

```
cus_ckp9mdxpd000i01ld6gzjgyl4 (reference a specific customer)
usr_ckp9me8zy000p01lda5579o3q (reference a specific user)
org_ckp9mek2d000s01ld8ffhhvd3 (reference a specific organisation)
```

#### Why?

By prefixing your entities IDs you can create a global namespace for your ID system which has numerous advantages:

* when displaying an ID you can immediately derive what type of object it represents from reading the prefix alone; most identifiers only showcase what instance is represented, but without information about the type it is machine-impossile to tell if ID `123` is from the `Dog` or `Cat` models. Whereas `cat_123` and `dog_123` make that clear.

* by having a global system of prefixing, you can speed up internal processes as (think: support) by having features in your backoffice such as "quick find" which allows you to dump the ID in question and be taken straight to the page which represents the specific instance of that type of object.

This may sound familiar, as it's how [Stripe](http://stripe.com/) handle their public IDs - everything is referenceable.

#### How?

Set a string literal during field instantiation. E.g:

```python
# models.py

class User(models.Model):
    public_id = CharIDField(prefix="usr_", ...)

>>> user = User.objects.create()
>>> user.public_id
"usr_ckp9me8zy000p01lda5579o3q"
```
## ๐Ÿ‘ฉโ€๐Ÿ’ป Development

### ๐Ÿ—๏ธ Local environment

The local environment is handled with `poetry`, so install that first then:

```
$ poetry install
```

### ๐Ÿงช Running tests

The tests themselves use `pytest` as the test runner.

After setting up the environment, run them using:

```
$ poetry run pytest
```

The full CI suite is controlled by `tox`, which contains a set of environments that will format
(`fmt`), lint, and test against all support Python + Django version combinations.

```
$ tox
```

#### โš™๏ธ CI

Uses GitHub Actions, see `./github/workflows`.

[cuid]: https://github.com/ericelliott/cuid
[ksuid]: https://github.com/segmentio/ksuid
[ulid]: https://github.com/ulid/spec

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/yunojuno/django-charid-field",
    "name": "django-charid-field",
    "maintainer": "YunoJuno",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "code@yunojuno.com",
    "keywords": "",
    "author": "YunoJuno",
    "author_email": "code@yunojuno.com",
    "download_url": "https://files.pythonhosted.org/packages/65/95/4b56b02d2985ed958ad2704fd037eb92e52f695ad42dd6d99ec313509c40/django_charid_field-0.4.tar.gz",
    "platform": null,
    "description": "# django-charid-field\n\n[![PyPI version](https://badge.fury.io/py/django-charid-field.svg)](https://badge.fury.io/py/django-charid-field)\n\nProvides a char-based, prefixable CharIDField for your Django models.\n\nIt can utilise [cuid], [ksuid], [ulid] or any other string-based UID generation systems.\n\nIt can be used as the primary key, or simple another key on your models.\n\n[cuid]: https://github.com/ericelliott/cuid\n[ksuid]: https://github.com/segmentio/ksuid\n[ulid]: https://github.com/ulid/spec\n\n## \u26f2 Feature set\n\n-   Ability to work with the UID generation spec of your choice.\n-   Support for prefixing the ID on a per-model basis \u00e0 la Stripe. e.g `cus_` => `cus_cjld2cjxh0000qzrmn831i7rn`\n-   Support for all database backends that support the `CharField`.\n-   Support for Python 3.8 & above only.\n\n## \ud83e\udd37 Why?\n\nTo get us a global namespace of collision-resistant IDs that:\n\n* are URL-safe\n* can be represented in a visual-space-efficient manor\n* are collision-resistant to allow for client side generation\n* exist now. UUID v6, v7, v8 are in RFC draft and not ready (Jul '21).\n\n[cuid], [ksuid], [ulid] & many others offer this now, and prefixing gets us the global namespace.\n\n**Why not use integers?**\n\n* Auto-incrementing integers are easily enumerable and give away collection count.\n\n* You can solve that with HashID but then you either have to store the HashID as another column or deal with constant conversion when looking up values in your UI VS raw in your database.\n\n* Most importantly: relying on your database to generate IDs means sequential writes. Your clients are not free to generate their own IDs without a round trip to the database.\n\n**Why not use UUIDs?**\n\nThey solve the collision problem so why not?\n\n* The text formats use hex, which is not visually space-efficient.\n* UUIDv4 (the one usually recommended) is completely random and thus impossible to sort. This has the knock on effect of making databases work harder when looking up/indexing as binary search goes out the window.\n* Optional hyphenation when representing the hex. This nuance results in more code.\n\n**Why prefix?**\n\nBecause global flat namespaces are powerful. An ID now represents the instance _and it's type_, which means you can have powerful lookup abilities with just the identifier alone. No more guessing whether `802302` is a `Dog` or a `Cat`.\n\n## \ud83d\udcd7 Install\n\nInstall using your favourite Python dependency manager, or straight with pip:\n\n```\npip install django-charid-field\n```\n\nYou'll also need to install your ID-generation library of choice (or bring your own).\n\nFor example:\n\n|UID Spec|Python Library|What could it look like? (with a prefix `dev_`)|\n|--------|--------------|----------------------------------------|\n|[cuid]|cuid.py: [GH](https://github.com/necaris/cuid.py) / [PyPi](https://pypi.org/project/cuid/)|`dev_ckpffbliw000001mi3fw42vsn`|\n|[ksuid]|cyksuid: [GH](https://github.com/timonwong/cyksuid) / [PyPi](https://pypi.org/project/cyksuid/)|`dev_1tOMP4onidzvnUFuTww2UeamY39`|\n|[ulid]|python-ulid: [GH](https://github.com/mdomke/python-ulid) / [PyPi](https://pypi.org/project/python-ulid/)|`dev_01F769XGM83VR75H86ZPHKK595`|\n\n\n\n## \u2728 Usage\n\n```\nfrom charidfield import CharIDField\n```\n\nWe recommend using `functool.partial` to create your own field for your codebase; this will allow you to specify your chosen ID generation and set the `max_length` parameter and then have an importable field you can use across all your models.\n\nHere's an example using the cuid spec and cuid.py:\n\n```python\n# Locate this somewhere importable\nfrom cuid import cuid\nfrom charidfield import CharIDField\n\nCuidField = partial(\n    CharIDField,\n    default=cuid,\n    max_length=30,\n    help_text=\"cuid-format identifier for this entity.\"\n)\n\n# models.py\nfrom wherever_you_put_it import CuidField\n\nclass Dog(models.Model):\n    id = CuidField(primary_key=True, prefix=\"dog_\")\n    name = models.CharField()\n\n# shell\n>>> dog = Dog(name=\"Ronnie\")\n>>> dog.id\n\"dog_ckpffbliw000001mi3fw42vsn\"\n\n```\n\n### Parameters\n\n|Param|Type|Required|Default|Note|\n|-----|----|--------|-------|----|\n|**default**|`Callable`|\u274c|-|This should be a callable which generates a UID in whatever system you chose. Your callable does not have to handle prefixing, the prefix will be applied onto the front of whatever string your default callable generates. Technically not required, but without it you will get blank fields and must handle ID generation yourself.|\n|**prefix**|`str` |\u274c|`\"\"`|If provided, the ID strings generated as the field's default value will be prefixed. This provides a way to have a per-model prefix which can be helpful in providing a global namespace for your ID system. The prefix should be provided as a string literal (e.g `cus_`). For more, see below.|\n|**max_length**|`int`|\u2705|Set it|Controls the maximum length of the stored strings. Provide your own to match whatever ID system you pick, remembering to take into account the length of any prefixes you have configured. Also note that there is no perf/storage impact for modern Postgres so for that backend it is effectively an arbitary char limit.|\n|**primary_key**|`boolean`|\u274c|`False`|Set to `True` to replace Django's default `Autofield` that gets used as the primary key, else the field will be additional ID field available to the model.|\n|**unique**|`boolean`|\u274c|`True`|Whether the field should be treated as unique across the dataset; the field provides a sane default of `True` so that a database index is setup to protext you against collisions (whether due to chance or, more likely, a bug/human error). To turn the index off, simply pass `False`.|\n\nAll other `django.db.models.fields.CharField` keyword arguments should work as expected. See the [Django docs](https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.CharField).\n\n### Usage as the Primary Key\n\n\nThis will replace Django's `AutoField` and the cuid will become the main primary key\nfor the entity, thus removing the default database-genererated incremental integer ID.\n\n```python\n# models/some_model.py or models.py\n\nclass SomeModel(models.Model):\n    id = CharIDField(primary_key=True, default=your_id_generator)\n\n>>> some_model = SomeModel.objects.create()\n>>> some_model.id\n\"ckp9jm3qn001001mrg5hw3sk4\"\n>>> some_model.pk\n\"ckp9jm3qn001001mrg5hw3sk4\"\n\"\"\n```\n### Setting up prefixing\n\n#### What?\n\nPrefixing allows per-entity ID namespacing, e.g:\n\n```\ncus_ckp9mdxpd000i01ld6gzjgyl4 (reference a specific customer)\nusr_ckp9me8zy000p01lda5579o3q (reference a specific user)\norg_ckp9mek2d000s01ld8ffhhvd3 (reference a specific organisation)\n```\n\n#### Why?\n\nBy prefixing your entities IDs you can create a global namespace for your ID system which has numerous advantages:\n\n* when displaying an ID you can immediately derive what type of object it represents from reading the prefix alone; most identifiers only showcase what instance is represented, but without information about the type it is machine-impossile to tell if ID `123` is from the `Dog` or `Cat` models. Whereas `cat_123` and `dog_123` make that clear.\n\n* by having a global system of prefixing, you can speed up internal processes as (think: support) by having features in your backoffice such as \"quick find\" which allows you to dump the ID in question and be taken straight to the page which represents the specific instance of that type of object.\n\nThis may sound familiar, as it's how [Stripe](http://stripe.com/) handle their public IDs - everything is referenceable.\n\n#### How?\n\nSet a string literal during field instantiation. E.g:\n\n```python\n# models.py\n\nclass User(models.Model):\n    public_id = CharIDField(prefix=\"usr_\", ...)\n\n>>> user = User.objects.create()\n>>> user.public_id\n\"usr_ckp9me8zy000p01lda5579o3q\"\n```\n## \ud83d\udc69\u200d\ud83d\udcbb Development\n\n### \ud83c\udfd7\ufe0f Local environment\n\nThe local environment is handled with `poetry`, so install that first then:\n\n```\n$ poetry install\n```\n\n### \ud83e\uddea Running tests\n\nThe tests themselves use `pytest` as the test runner.\n\nAfter setting up the environment, run them using:\n\n```\n$ poetry run pytest\n```\n\nThe full CI suite is controlled by `tox`, which contains a set of environments that will format\n(`fmt`), lint, and test against all support Python + Django version combinations.\n\n```\n$ tox\n```\n\n#### \u2699\ufe0f CI\n\nUses GitHub Actions, see `./github/workflows`.\n\n[cuid]: https://github.com/ericelliott/cuid\n[ksuid]: https://github.com/segmentio/ksuid\n[ulid]: https://github.com/ulid/spec\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Provides a char-based, prefixable ID field for your Django models. Supports cuid, ksuid, ulid, et al.",
    "version": "0.4",
    "project_urls": {
        "Documentation": "https://github.com/yunojuno/django-charid-field/blob/master/README",
        "Homepage": "https://github.com/yunojuno/django-charid-field",
        "Repository": "https://github.com/yunojuno/django-charid-field"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7053368241098a9666189ce15653255777c7a34c095d49224edb02df1298882f",
                "md5": "87469b546225fecafed363f0d4c45d88",
                "sha256": "70f140cb15ddde8459fc5a6cd8c4d24ed08d4c2aac2212d24df0ac724bc411f4"
            },
            "downloads": -1,
            "filename": "django_charid_field-0.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "87469b546225fecafed363f0d4c45d88",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 7584,
            "upload_time": "2023-11-15T11:17:41",
            "upload_time_iso_8601": "2023-11-15T11:17:41.807539Z",
            "url": "https://files.pythonhosted.org/packages/70/53/368241098a9666189ce15653255777c7a34c095d49224edb02df1298882f/django_charid_field-0.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "65954b56b02d2985ed958ad2704fd037eb92e52f695ad42dd6d99ec313509c40",
                "md5": "1b6e5ff81dadb17f4a3764c61877226b",
                "sha256": "3d8a0f4395f4c9b19667800254924503016160051c166c61e935e7366036cd38"
            },
            "downloads": -1,
            "filename": "django_charid_field-0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "1b6e5ff81dadb17f4a3764c61877226b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 7089,
            "upload_time": "2023-11-15T11:17:43",
            "upload_time_iso_8601": "2023-11-15T11:17:43.572638Z",
            "url": "https://files.pythonhosted.org/packages/65/95/4b56b02d2985ed958ad2704fd037eb92e52f695ad42dd6d99ec313509c40/django_charid_field-0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-11-15 11:17:43",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "yunojuno",
    "github_project": "django-charid-field",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "django-charid-field"
}
        
Elapsed time: 0.15247s