非侵入式hashids库Django

django-hashids的Python项目详细描述


Django哈希ID

Github ActionsCode CoveragePython VersionPyPI PackageLicense

django hashids是django的一个简单的、非侵入性的hashids库。它充当一个模型字段,但它不接触数据库或更改模型。在

安装

pip install django-hashids

django-hashids是用django1.11、2.2、3.0、3.1和python3.6、3.7、3.8、3.9测试的。在

使用

向任何模型添加HashidsField

^{pr2}$

TestModel.hashid字段将代理TestModel.id字段,但所有查询都将返回并接收哈希ID字符串。TestModel.id将像以前一样工作。在

示例

instance=TestModel.objects.create()instance2=TestModel.objects.create()instance.id# 1instance2.id# 2# Allows access to the fieldinstance.hashid# '1Z'instance2.hashid# '4x'# Allows querying by the fieldTestModel.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.valuesTestModel.objects.values_list("hashid",flat=True)# ["1Z", "4x"]TestModel.objects.filter(hashid__in=TestModel.objects.values("hashid"))

配置

可以在设置文件中添加以下属性来设置HashidsField的默认参数:

  1. DJANGO_HASHIDS_SALT:默认salt
  2. DJANGO_HASHIDS_MIN_LENGTH:默认最小长度
  3. DJANGO_HASHIDS_ALPHABET:默认字母表

HashidsField不需要任何参数,但可以提供以下参数来修改其行为。在

NameDescription
^{}The proxied field name
^{}The hashids instance used to encode/decode for this field
^{}The salt used for this field to generate hashids
^{}The minimum length of hashids generated for this field
^{}The alphabet used by this field to generate hashids

参数hashids_instancesaltmin_length和{}互斥。有关参数的详细信息,请参见hashids-python。在

还支持一些常见的模型参数,如verbose_name。在

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java MongoOperations Upert/FindModify删除mongo中的所有字段   java括号中的二叉树,而!=空值无效   java com.*.*.**。安全CustomUserDetail无法强制转换为com。***。安全CustomUserDetail   java如何编写多线程算法来发现可用socket   java Spring Boot Solr:子文档的索引列表   java如何向maven shade插件添加外部jar文件   java zebra条形码扫描仪Android studio集成   java为什么一个地方的原始类型会导致其他地方的通用调用站点被视为原始类型?   java将swing控件拆分为相等的部分   java如何在jshell中为spring项目设置类路径   java关于如何从API级别19的时间选择器中删除此白边的提示?   存储二进制字符串所有可能序列的算法,Java   测试JUnitJava。lang.NullPointerException   java Google AppEngine数据库   JavaSpring如何向数据对象注入@Value?   在eclipse java项目中包含mavenant构建库   带有JSP/EL的java省略号(缩写文本)