django-hashid-field is a Django library that provides model fields for obfuscating database primary keys using Hashids. It converts integer IDs into short, unique, non-sequential string identifiers, enhancing data privacy and preventing enumeration attacks. The current version is 3.4.1 and it generally follows a release cadence tied to Django LTS releases and feature enhancements.
pip install django-hashid-fieldVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to define models using `HashidAutoField` for primary keys and `HashidField` for other fields. It also highlights the required `HASHID_FIELD_SALT` setting and how to enable integer lookups with `HASHID_FIELD_ALLOW_INT_LOOKUP`.
Upgrade Django to 3.2+ and Python to 3.8+. For REST Framework integration, install and use `django-hashid-rest-framework`.
Update `HashidPrimaryKey` to `HashidAutoField` in your models. If you need to maintain compatibility with existing hashids, explicitly configure `HASHID_FIELD_SALT` and `HASHID_FIELD_ALPHABET` in your Django settings to match your previous setup.
Add a unique, secret string to your Django `settings.py`, e.g., `HASHID_FIELD_SALT = 'your-super-secret-salt-here'`.
To enable integer lookups (e.g., `MyModel.objects.get(id=123)`), set `HASHID_FIELD_ALLOW_INT_LOOKUP = True` in your Django settings. Otherwise, always query using the hashid string (e.g., `MyModel.objects.get(pk='abCdef')`).