https://github.com/stabldev/django-shortuuid
A modern, Django model field for generating short, unique, URL-safe UUIDs using the shortuuid lib.
https://github.com/stabldev/django-shortuuid
Last synced: 8 months ago
JSON representation
A modern, Django model field for generating short, unique, URL-safe UUIDs using the shortuuid lib.
- Host: GitHub
- URL: https://github.com/stabldev/django-shortuuid
- Owner: stabldev
- License: mit
- Created: 2025-09-28T08:34:32.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2025-09-29T03:07:20.000Z (9 months ago)
- Last Synced: 2025-09-29T04:16:31.402Z (9 months ago)
- Language: Python
- Size: 21.5 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# django-shortuuid




A drop-in Django model field for generating short, URL-safe, and unique IDs using [shortuuid](https://github.com/skorokithakis/shortuuid).
Customizable, migration-friendly, with optional collision detection and Django admin integration.
## Features
- Short, URL-safe unique IDs for Django models (default 22 characters, configurable)
- Customizable alphabet, prefix, and length
- Optional database collision detection and retry logic
- Django admin: field is read-only but visible
- Compatible with Django migrations
- Python type hints
## Installation
```
pip install django-shortuuid
```
## Usage
```py
from django.db import models
from django_shortuuid.fields import ShortUUIDField
class MyModel(models.Model):
id = ShortUUIDField(primary_key=True, prefix="id-")
# ... other fields ...
```
By default, `auto=True` generates a unique short UUID when each instance is saved, and makes the field read-only in forms/admin.
## Example
See [django_example/](django_example/) for a full Django setup and admin usage.
## Configuration
- `auto`: Generate and set value automatically (`True` by default)
- `length`: Length of the generated unique string (default `22`)
- `prefix`: Optional string prefix for the field value
- `alphabet`: Custom alphabet for uuid generation (default: `shortuuid`’s default)
- `collision_check`: Enables collision checking in the database (`True` by default)
- `max_retries`: Max attempts to generate unique value before error (`10` by default)
## License
[MIT](LICENSE) Copyright (c) [stabldev](https://github.com/stabldev)
## Credits
This project was inspired by [benrobster/django-shortuuidfield](https://github.com/benrobster/django-shortuuidfield)
and builds on ideas from the original implementation.
Special thanks to [shortuuid](https://github.com/skorokithakis/shortuuid) for the short, URL-safe UUID generation.