django-prefix-id


Namedjango-prefix-id JSON
Version 1.0.0 PyPI version JSON
download
home_page
SummaryA simple prefix ID Django model field, using UUID to Base62 encoding.
upload_time2023-10-14 01:15:55
maintainer
docs_urlNone
author
requires_python>=3.8
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ### Overview

`django-prefix-id` is a simple package that contains a single Django model 
field, `PrefixIDField`. `PrefixIDField` is
- based on Django's `CharField`
- allows using a prefix (recommended for context)
  - enables some very interesting product usage and querying patterns 
- autogenerates a unique id by default (auto-including the prefix, if provided)
  - uses `uuid.uuid4()` to generate a 128-bit integer, which is then 
    base62-encoded 
- auto-sets the `max_length` field property, factoring in the provided prefix length 
  and a known max length of 22 characters for a base62-encoded 128-bit integer

**Why work with IDs like `7584358` or `'7fca5f1d-f867-4696-960b-e57274cc5647'` 
when you could have something swanky like `'user_4wr7jBDTEqVBCsXEih4zfP'`?**

If you already like to use UUIDs as IDs, you should use `django-prefix-id`! If you currently use
autoincrementing IDs, you should also consider using `django-prefix-id`, from an obfuscation 
point of view.

### Installation

```shell
$ pip install django-prefix-id
```

### Usage
```python
from prefix_id import PrefixIDField

class Book(models.Model):
    id = PrefixIDField(prefix="book", primary_key=True)
    title = models.CharField(max_length=100, blank=False)
    ...
```

Make your migrations, as you normally would:
```shell
$ python manage.py makemigrations
```

### Development

```shell
$ pip -m venv venv
$ source /venv/bin/activate
$ pip install -r requirements.txt
```

### Testing

Manually run tests:
```shell
$ pytest
```

Test coverage is at 100%. To get test coverage output:
```shell
$ pytest --cov --cov-report html
# OR
$ pytest --cov
```

To cover a breadth of likely usage scenarios, `tox` is the tool of 
choice. It is set up with a matrix covering various combinations of 
Python versions 3.8 - 3.12 with Django 4.2 and 5.0.

See `tox.ini` to enable testing Django versions as far back as Django 
3.2 - tests pass across the board, but for simplicity only latest LTS 
Django and other currently supported versions will be considered in 
scope.

Run the test suite against all enabled versions:

```shell
$ tox
```

### Backstory and Inspiration

Stripe-like prefixed entity IDs have long been a topic of interest to me, 
and the improved user experience that they can provide. I'd gone through 
a couple of iterations with FastAPI and Django - and thought I might as 
well release something that I'm happy with sharing publicly.

About 80% through this effort, I found yunojuno's 
[django-charid-field](https://github.com/yunojuno/django-charid-field/) 
project, and took some further inspiration from it - so thanks to them! 

(It also appears we have other project interests in common!)

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "django-prefix-id",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "",
    "author": "",
    "author_email": "James Addison <addi00@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/f2/a0/62fdf16122109081cb8e6662ca859ba99f84e05d4734fe07a7a24cc06a7f/django-prefix-id-1.0.0.tar.gz",
    "platform": null,
    "description": "### Overview\n\n`django-prefix-id` is a simple package that contains a single Django model \nfield, `PrefixIDField`. `PrefixIDField` is\n- based on Django's `CharField`\n- allows using a prefix (recommended for context)\n  - enables some very interesting product usage and querying patterns \n- autogenerates a unique id by default (auto-including the prefix, if provided)\n  - uses `uuid.uuid4()` to generate a 128-bit integer, which is then \n    base62-encoded \n- auto-sets the `max_length` field property, factoring in the provided prefix length \n  and a known max length of 22 characters for a base62-encoded 128-bit integer\n\n**Why work with IDs like `7584358` or `'7fca5f1d-f867-4696-960b-e57274cc5647'` \nwhen you could have something swanky like `'user_4wr7jBDTEqVBCsXEih4zfP'`?**\n\nIf you already like to use UUIDs as IDs, you should use `django-prefix-id`! If you currently use\nautoincrementing IDs, you should also consider using `django-prefix-id`, from an obfuscation \npoint of view.\n\n### Installation\n\n```shell\n$ pip install django-prefix-id\n```\n\n### Usage\n```python\nfrom prefix_id import PrefixIDField\n\nclass Book(models.Model):\n    id = PrefixIDField(prefix=\"book\", primary_key=True)\n    title = models.CharField(max_length=100, blank=False)\n    ...\n```\n\nMake your migrations, as you normally would:\n```shell\n$ python manage.py makemigrations\n```\n\n### Development\n\n```shell\n$ pip -m venv venv\n$ source /venv/bin/activate\n$ pip install -r requirements.txt\n```\n\n### Testing\n\nManually run tests:\n```shell\n$ pytest\n```\n\nTest coverage is at 100%. To get test coverage output:\n```shell\n$ pytest --cov --cov-report html\n# OR\n$ pytest --cov\n```\n\nTo cover a breadth of likely usage scenarios, `tox` is the tool of \nchoice. It is set up with a matrix covering various combinations of \nPython versions 3.8 - 3.12 with Django 4.2 and 5.0.\n\nSee `tox.ini` to enable testing Django versions as far back as Django \n3.2 - tests pass across the board, but for simplicity only latest LTS \nDjango and other currently supported versions will be considered in \nscope.\n\nRun the test suite against all enabled versions:\n\n```shell\n$ tox\n```\n\n### Backstory and Inspiration\n\nStripe-like prefixed entity IDs have long been a topic of interest to me, \nand the improved user experience that they can provide. I'd gone through \na couple of iterations with FastAPI and Django - and thought I might as \nwell release something that I'm happy with sharing publicly.\n\nAbout 80% through this effort, I found yunojuno's \n[django-charid-field](https://github.com/yunojuno/django-charid-field/) \nproject, and took some further inspiration from it - so thanks to them! \n\n(It also appears we have other project interests in common!)\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "A simple prefix ID Django model field, using UUID to Base62 encoding.",
    "version": "1.0.0",
    "project_urls": {
        "Bug Tracker": "https://github.com/jaddison/django-prefix-id/issues",
        "Homepage": "https://github.com/jaddison/django-prefix-id"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b3cbe91df36edcb8ab779e2d10decac6c3b3f858f4312a1a18d9f7f08b147b24",
                "md5": "e3c004d5fe86a187604da86f4f50dd54",
                "sha256": "8473d407ba7c9a7c4f98ac8fdd802b5a4d0927a296774052bd04a59455e0451b"
            },
            "downloads": -1,
            "filename": "django_prefix_id-1.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e3c004d5fe86a187604da86f4f50dd54",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 5607,
            "upload_time": "2023-10-14T01:15:53",
            "upload_time_iso_8601": "2023-10-14T01:15:53.830992Z",
            "url": "https://files.pythonhosted.org/packages/b3/cb/e91df36edcb8ab779e2d10decac6c3b3f858f4312a1a18d9f7f08b147b24/django_prefix_id-1.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f2a062fdf16122109081cb8e6662ca859ba99f84e05d4734fe07a7a24cc06a7f",
                "md5": "a24bf5d4f09fca907ed4ea9c803937cd",
                "sha256": "ecb2776f63ca7d1f4ce2bdceae3962244fd3801eb28a644acbaeafa520f77468"
            },
            "downloads": -1,
            "filename": "django-prefix-id-1.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "a24bf5d4f09fca907ed4ea9c803937cd",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 5253,
            "upload_time": "2023-10-14T01:15:55",
            "upload_time_iso_8601": "2023-10-14T01:15:55.813504Z",
            "url": "https://files.pythonhosted.org/packages/f2/a0/62fdf16122109081cb8e6662ca859ba99f84e05d4734fe07a7a24cc06a7f/django-prefix-id-1.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-14 01:15:55",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "jaddison",
    "github_project": "django-prefix-id",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "tox": true,
    "lcname": "django-prefix-id"
}
        
Elapsed time: 0.13302s