Django Hashids
django-hashids is a simple and non-intrusive hashids library for Django. It acts as a model field, but it does not touch the database or change the model.
Features
- Proxy the internal model
pkfield without storing the value in the database. - Allows lookups and filtering by hashid string.
- Can be used as sort key
- Allows specifying a salt, min_length and alphabet globally
- Supports custom salt, min_length, and alphabet per field
- Supports Django REST Framework Serializers
- Supports exact ID searches in Django Admin when field is specified in search_fields.
- Supports common filtering lookups, such as __iexact, __contains, __icontains, though matching is the same as __exact.
- Supports other lookups: isnull, gt, gte, lt and lte.
Install
pip install django-hashids
django-hashids is tested with Django 1.11, 2.2, 3.0, 3.1, 3.2, 4.0 and python 3.6, 3.7, 3.8, 3.9, 3.10.
Usage
Add HashidsField to any model
from django_hashids import HashidsField
class TestModel(Model):
hashid = HashidsField(real_field_name="id")
TestModel.hashid field will proxy TestModel.id field but all queries will return and receive hashids strings. TestModel.id will work as before.
Examples
instance = TestModel.objects.create()
instance2 = TestModel.objects.create()
instance.id # 1
instance2.id # 2
# Allows access to the field
instance.hashid # '1Z'
instance2.hashid # '4x'
# Allows querying by the field
TestModel.objects.get(hashid="1Z")
TestModel.objects.filter(hashid="1Z")
TestModel.objects.filter(hashid__in=["1Z", "4x"])
TestModel.objects.filter(hashid__gt="1Z") # same as id__gt=1, would return instance 2
# Allows usage in queryset.values
TestModel.objects.values_list("hashid", flat=True) # ["1Z", "4x"]
TestModel.objects.filter(hashid__in=TestModel.objects.values("hashid"))
Using with URLs
You can use hashids to identify items in your URLs by treating them as slugs.
In urls.py:
urlpatterns = [
path("item/<slug>/", YourDetailView.as_view(), name="item-detail"),
]
And in your view:
class YourDetailView(DetailView):
model = Item
slug_field = 'hashid'
Config
The folloing attributes can be added in settings file to set default arguments of HashidsField:
DJANGO_HASHIDS_SALT: default saltDJANGO_HASHIDS_MIN_LENGTH: default minimum lengthDJANGO_HASHIDS_ALPHABET: default alphabet
HashidsField does not reqiure any arguments but the followinig arguments can be supplied to modify its behavior.
| Name | Description |
|---|---|
real_field_name |
The proxied field name |
hashids_instance |
The hashids instance used to encode/decode for this field |
salt |
The salt used for this field to generate hashids |
min_length |
The minimum length of hashids generated for this field |
alphabet |
The alphabet used by this field to generate hashids |
The argument hashids_instance is mutually exclusive to salt, min_length and alphabet. See hashids-python for more info about the arguments.
Some common Model arguments such as verbose_name are also supported.
Metadata
Release files for django-hashids 0.7.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django_hashids-0.7.1.tar.gz | 4.8 kB | Without provenance | Details for django_hashids-0.7.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_hashids-0.7.1-py3-none-any.whl | Python 3 | none | any | Details for django_hashids-0.7.1-py3-none-any.whl |
Total release size: 10.5 kB
Release files / django_hashids-0.7.1.tar.gz
| Download URL | django_hashids-0.7.1.tar.gz |
|---|---|
| Size | 4.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e3874d782f1c90a435f5ecc918adc2971453856a11fd60b0f0e8289850c1b787
|
|
BLAKE2b-256 checksum How to use checksums |
841c59a9425f98f9f50605122c6b08f9ce944b6f7d6a99b33fbd30bffe932185
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.3 CPython/3.12.11 Linux/6.12.55
|
Release files / django_hashids-0.7.1-py3-none-any.whl
| Download URL | django_hashids-0.7.1-py3-none-any.whl |
|---|---|
| Size | 5.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
dced9c79a27770d2f9bacf4eb94d04330e25d55b8a4c633fb93144482c9dde38
|
|
BLAKE2b-256 checksum How to use checksums |
a5b70096c0d0059e3edc546ffcdb201bf793107ec325806957a66c827d2923fb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.3 CPython/3.12.11 Linux/6.12.55
|
Security for django-hashids 0.7.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.