跳转到主要内容

适用于Django 1.8及以上版本的简短UUID字段

项目描述

https://img.shields.io/pypi/v/django-smalluuid.svg https://img.shields.io/pypi/dm/django-smalluuid.svg https://img.shields.io/github/license/adamcharnock/django-smalluuid.svg https://img.shields.io/travis/adamcharnock/django-smalluuid.svg https://coveralls.io/repos/adamcharnock/django-smalluuid/badge.svg?branch=master

安装

使用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

致谢

django-smalluuid是用seed打包的,并依赖于smalluuid

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分布

django-smalluuid-1.2.1.tar.gz (8.8 kB 查看哈希值)

上传于 来源

由以下机构支持