适用于Django 1.8及以上版本的简短UUID字段
项目描述
安装
使用pip安装
pip install django-smalluuid
测试平台
Django >= 2.2 <= 4.0
Python 3.7, 3.8, 3.9, 3.10
基本用法
要开始使用,请在模型定义中使用SmallUUIDField字段
from django.db import models
from django_smalluuid.models import SmallUUIDField, uuid_default
class ExampleModel(models.Model):
uuid = SmallUUIDField(default=uuid_default())
该字段提供SmallUUID实例的值(参见smalluuid)
>>> obj = ExampleModel.objects.create()
# The initial UUID has been auto-generated by uuid_default()
>>> obj.uuid
SmallUUID('T1q_P6HcQNSyW6tpqJTxww')
# It is still available in the groupex hex form (if needed)
>>> obj.hex_grouped
'4f5abf3f-a1dc-40d4-b25b-ab69a894f1c3'
# Filtering is done on the shortened UUIDs
>>> ExampleModel.objects.filter(uuid='T1q_P6HcQNSyW6tpqJTxww')
[<ExampleModel: ExampleModel object>]
类型用法
django-smalluuid也支持由smalluuid提供的类型UUID。这允许将对象的类型存储在UUID中。
更新上述示例
from django.db import models
from django_smalluuid.models import SmallUUIDField, uuid_typed_default
class TypedExampleModel(models.Model):
uuid = SmallUUIDField(default=uuid_typed_default(type=42))
可以交互如下
>>> obj = TypedExampleModel.objects.create()
>>> obj.uuid
TypedSmallUUID('qvyk8nzbQfu8zAnTPQweyw')
>>> obj.uuid.type
42