Non-intrusive hashids library for Django
Project description
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
pk
field 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"))
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-hashids-0.6.0.tar.gz
.
File metadata
- Download URL: django-hashids-0.6.0.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.2 Darwin/21.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a698975566d07f68087462f5657441e9e944f66398208a11a2ca5c6446be1a4 |
|
MD5 | 4cf83c50bacfe4a94b202ad16ad7ff2c |
|
BLAKE2b-256 | e734442efca8d2a60d73dfbeda3ad6aca39d4f506b9fc0028030ab2eb5e39e91 |
File details
Details for the file django_hashids-0.6.0-py3-none-any.whl
.
File metadata
- Download URL: django_hashids-0.6.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.2 Darwin/21.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd80a72b364dd0c06343533421f204a13d9e9a51c576a0be17aaeea511664fe4 |
|
MD5 | 97e67c82a2016199ed1df67c804d052f |
|
BLAKE2b-256 | 1050c35657e4508817beb875e8ec503a806292b87f0f92d6d861cbf5075aeeb3 |