Skip to main content

Non-intrusive hashids library Django

Project description

Django Hashids

Github Actions Code Coverage Python Version PyPI Package License

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, 3.1 and python 3.6, 3.7, 3.8, 3.9.

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("hashid"))

Config

The folloing attributes can be added in settings file to set default arguments of HashidsField:

  1. DJANGO_HASHIDS_SALT: default salt
  2. DJANGO_HASHIDS_MIN_LENGTH: default minimum length
  3. DJANGO_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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-hashids-0.3.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

django_hashids-0.3.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file django-hashids-0.3.0.tar.gz.

File metadata

  • Download URL: django-hashids-0.3.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.5 Linux/5.8.6-1-MANJARO

File hashes

Hashes for django-hashids-0.3.0.tar.gz
Algorithm Hash digest
SHA256 8dfe5344ba1f4a6ed76f1fcd71b9c6788d68ba0da3bf1b22cb1c6a8a61888405
MD5 4cc7a5904abb5bfd281d13dbee8eb3a7
BLAKE2b-256 bea6e4036fe3ba05b862e13f2c4f9f5141f21cf5e78651c9cdcdcaec6eb1e366

See more details on using hashes here.

File details

Details for the file django_hashids-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: django_hashids-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.5 Linux/5.8.6-1-MANJARO

File hashes

Hashes for django_hashids-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8232a8c8bb24806cf874ce0c4da63c29de49c85a3c6b59aa97330a805cc385b8
MD5 a47b275662ce656676d5439ee49744d3
BLAKE2b-256 3b481e6342799590f70bea594eed524404fa0c3a67780cae7fa563c0d0814f21

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page