Non-intrusive sqids library for Django
Low maintenance activity. Last release 17 months ago, commits in 0 of the last 26 weeks. Weaker on commit activity and recent releases.
pip install django-sqidsDjango Sqids
django-sqids is a simple and non-intrusive sqids library for Django. It acts as a model field, but it does not touch the database or change the model.
The project was forked from django-hashids to provide the same functionality with the newer Sqids library.
Features
- Proxy the internal model
pkfield without storing the value in the database. - Allows lookups and filtering by sqid string.
- Can be used as sort key.
- Allows specifying a min_length and alphabet globally.
- Supports custom min_length, prefix, and alphabet per field.
- Supports Django REST Framework Serializers.
- Supports exact ID searches in Django Admin when field is specified in search_fields.
- Supports other lookups:
in,isnull,gt,gte,lt, andlte.
Install
pip install django-sqids
django-sqids is tested with Django 3.2, 4.2, 5.0 and Python 3.8 - 3.12.
Usage
Add SqidsField to any model
from django_sqids import SqidsField
class TestModel(Model):
sqid = SqidsField(real_field_name="id")
TestModel.sqid field will proxy TestModel.id field but all queries will return and receive sqids 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.sqid # '1Z'
instance2.sqid # '4x'
# Allows querying by the field
TestModel.objects.get(sqid="1Z")
TestModel.objects.filter(sqid="1Z")
TestModel.objects.filter(sqid__in=["1Z", "4x"])
TestModel.objects.filter(sqid__gt="1Z") # same as id__gt=1, would return instance 2
# Allows usage in queryset.values
TestModel.objects.values_list("sqid", flat=True) # ["1Z", "4x"]
TestModel.objects.filter(sqid__in=TestModel.objects.values("sqid"))