django 1.8及以上版本的简短uuid字段
django-smalluuid的Python项目详细描述
安装
使用PIP安装:
pip install django-smalluuid
测试时间:
- Django >= 1.8 <= 1.11
- Python 2.7, 3.4, 3.5, 3.6
基本用法
要开始使用模型定义中的SmallUUIDField字段:
fromdjango.dbimportmodelsfromdjango_smalluuid.modelsimportSmallUUIDField,uuid_defaultclassExampleModel(models.Model):uuid=SmallUUIDField(default=uuid_default())
该字段提供值作为SmallUUID的实例(请参见smalluuid):
>>>obj=ExampleModel.objects.create()# The initial UUID has been auto-generated by uuid_default()>>>obj.uuidSmallUUID('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:ExampleModelobject>]
键入用法
django-smalluuid还支持由smalluuid提供的类型化uuid。这个 允许对象的类型存储在uuid中。
更新以上示例:
fromdjango.dbimportmodelsfromdjango_smalluuid.modelsimportSmallUUIDField,uuid_typed_defaultclassTypedExampleModel(models.Model):uuid=SmallUUIDField(default=uuid_typed_default(type=42))
可与以下内容交互:
>>>obj=TypedExampleModel.objects.create()>>>obj.uuidTypedSmallUUID('qvyk8nzbQfu8zAnTPQweyw')>>>obj.uuid.type42