Non-intrusive hashids library 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.
Install
pip install django-hashids
django-hashids
is tested with Django 1.11, 2.2, 3.0 and python 3.6, 3.7, 3.8.
Usage
Add HashidsField
to any model
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("hashids"))
Config
DJANGO_HASHIDS_SALT
can be set in Django's settinigs to be used as the default salt.
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.1.2.tar.gz
.
File metadata
- Download URL: django-hashids-0.1.2.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.0 CPython/3.7.7 Darwin/18.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | da4202edf9c63ca3bff5375c086c4362dbc4837363a3c90000cea64711d0d9c8 |
|
MD5 | 03d39588288162cf57d499a9ea309746 |
|
BLAKE2b-256 | db0845569dc30e723d898a97f805d15ebde05bc729a6ea2c5d7b3480b8162f9e |
File details
Details for the file django_hashids-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: django_hashids-0.1.2-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.0 CPython/3.7.7 Darwin/18.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 675082f7d3d536c624451fe9cf077f3d790070047470b1b256c54b8033c1e8d2 |
|
MD5 | f9145894988557d4915e76a485cc3c9e |
|
BLAKE2b-256 | 6f74d0beaa25406952ca56a37b73af26858a277d93c9593cc83d7313db094f8f |