Package: django-sqids
Django Sqidsdjango-sqids is a simple and non-intrusivesqidslibrary for Django. It acts as a model field, but it does not touch the database or change the model.The project was forked fromdjango-hashidsto provide the same functionality with the newer Sqids library.FeaturesProxy the internal modelpkfield without storing the value in the database.Allows lookups and filtering by sqid string.Can be used as sort keyAllows specifying a min_length and alphabet globallySupports custom min_length, and alphabet per fieldSupports Django REST Framework SerializersSupports 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.Installpipinstalldjango-sqidsdjango-sqidsis tested with Django 3.2, 4.2, 5.0 and Python 3.8 - 3.12.UsageAddSqidsFieldto any modelfromdjango_sqidsimportSqidsFieldclassTestModel(Model):sqid=SqidsField(real_field_name="id")TestModel.sqidfield will proxyTestModel.idfield but all queries will return and receive sqids strings.TestModel.idwill work as before.Examplesinstance=TestModel.objects.create()instance2=TestModel.objects.create()instance.id# 1instance2.id# 2# Allows access to the fieldinstance.sqid# '1Z'instance2.sqid# '4x'# Allows querying by the fieldTestModel.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.valuesTestModel.objects.values_list("sqid",flat=True)# ["1Z", "4x"]TestModel.objects.filter(sqid__in=TestModel.objects.values("sqid"))Using with URLsYou can use sqids to identify items in your URLs by treating them as slugs.Inurls.py:urlpatterns=[path("item/<slug>/",YourDetailView.as_view(),name="item-detail"),]And in your view:classYourDetailView(DetailView):model=Itemslug_field='sqid'Using with Django AdminAdd the field to your ModelAdmin'ssearch_fieldsto quickly find a record by its Sqid:classMyModelAdmin(admin.ModelAdmin):search_fields=["sqid__exact",]ConfigThe folloing attributes can be added in settings file to set default arguments ofSqidsField:DJANGO_SQIDS_MIN_LENGTH: default minimum lengthDJANGO_SQIDS_ALPHABET: default alphabetSqidsFielddoes not reqiure any arguments but the following arguments can be supplied to modify its behavior.NameDescriptionreal_field_nameThe proxied field namesqids_instanceThe sqids instance used to encode/decode for this fieldmin_lengthThe minimum length of sqids generated for this fieldalphabetThe alphabet used by this field to generate sqidsThe argumentsqids_instanceis mutually exclusive tomin_lengthandalphabet. Seesqids-pythonfor more info about the arguments.Some common Model arguments such asverbose_nameare also supported.Where did the Salt go?Sqids removed the "salt" parameterto prevent association with security or safety.django_sqidsprovides a usefulshuffle_alphabetfunction that helps reintroduce the same idea:fromdjango_sqidsimportSqidsField,shuffle_alphabetclassMyModel(models.Model):# will use your configured default alphabetsqid=SqidsField(alphabet=shuffle_alphabet(seed='randomSeed'))classHexModel(models.Model):sqid=SqidsField(alphabet=shuffle_alphabet(seed='randomSeed',alphabet='0123456789abcdef'))