Django UUIDField如何在Postgresql中生成UUID? [英] How does a Django UUIDField generate a UUID in Postgresql?

查看:161
本文介绍了Django UUIDField如何在Postgresql中生成UUID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读此博客文章后 https:// blog.starkandwayne.com/2015/05/23/uuid-primary-keys-in-postgresql/

我想了解有关Django如何生成的更多信息uuid,因为我将它们用作我的pk。好吧,根据文档, https://docs.djangoproject.com /es/1.9/ref/models/fields/#uuidfield ,Django依赖于Python UUID模块 https://docs.python.org/3/library/uuid.html#uuid.UUID 。但是UUID有很多种,我完全不清楚在Django中生成的是哪个,或者如何选择(假设有一个选择)。

I wanted to know more about how Django generates uuid because I am using them as my pk. Well, according to the docs, https://docs.djangoproject.com/es/1.9/ref/models/fields/#uuidfield, Django is relying on the Python UUID module https://docs.python.org/3/library/uuid.html#uuid.UUID. But there are many kinds of UUID, and it is not at all clear to me which one is being generated in Django, or how to chose, assuming a choice is available.

最后,考虑到博客文章中指出的碎片问题,并假定 uuid_generate_v1mc 在Python中不直接可用还是Django,有没有办法强迫他们使用它?

Finally, given the fragmentation issue pointed out in the blog post, and assuming uuid_generate_v1mc is not available directly in Python or Django, is there a way to force them to use it?

推荐答案



  • Django和Python如何生成一个PostgreSQL中的UUID?

  • How does Django and or Python generate a UUID in Postgresql?

但是UUID有很多种,我还不清楚在Django中生成的是哪种UUID

But there are many kinds of UUID, and it is not at all clear to me which one is being generated in Django

使用 UUIDField 作为Django中的主键,它不会为您生成一个UUID,而是您在保存对象之前自行生成的UUID

When you use UUIDField as a primary key in Django, it doesn't generate a UUID one for you, you generate it yourself before you save the object

我不不知道此后是否发生了变化,但是上次我使用 UUIDField 时,您必须自己指定UUID值(例如,创建对象时,Django不会让您使用空白UUID保存对象,并让数据库生成一个)。查看Django文档样本可以增强我的想法,因为它们提供了 default = uuid.uuid4()例如

I don't know if things have changed since, but last time I have used a UUIDField, you had to specify the UUID value yourself (e.g. when you create the object, Django won't let you save an object with a blank UUID and have the database generate one). Looking at the Django documentation samples reinforces my thought, because they provide a default=uuid.uuid4() e.g. in the primary key.

class MyUUIDModel(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
                                                    ^
                                                    |__ calls uuid.uuid4() 



要选择哪个UUID版本



要比较不同UUID版本的属性,请参见以下问题:要使用哪个UUID版本?

对于很多应用程序,UUID4都很好

如果只想生成一个UUID并继续生活,那么像上面的代码片段一样, uuid.uuid4()就可以了。 UUID4是随机的UUID,发生碰撞的机会非常少,您根本不必担心,特别是如果您每秒未生成大量的UUID。

If you just want to generate a UUID and get on with your life, uuid.uuid4() like the snippet above is just fine. UUID4 is a random UUID and the chances of a collision are so remote that you don't really need to worry about, especially if you're not generating a ton of them per second.


最后,考虑到博客文章中指出的碎片问题,并假定 uuid_generate_v1mc 在Python或Django中不直接可用,是有没有办法强迫他们使用它?

Finally, given the fragmentation issue pointed out in the blog post, and assuming uuid_generate_v1mc is not available directly in Python or Django, is there a way to force them to use it?

具有随机MAC地址的Python UUID1,例如 uuid-ossp uuid_generate_v1mc

A Python UUID1 with random MAC address, like uuid-ossp's uuid_generate_v1mc

您链接的博客提到了UUID1的使用。 Python的 uuid.uuid1()使用一个参数代替默认的实际硬件MAC地址(48位)。由于这些随机位是UUID1的末尾,因此UUID1的前几位可以基于顺序/时间戳,以限制索引碎片。

The blog you linked mentions the use of UUID1. Python's uuid.uuid1() takes a parameter that is used instead of the default real hardware MAC address (48 bits). Because these random bits are the end of the UUID1, the first bits of the UUID1 can be sequential/timestamp-based to limit the index fragmentation.

所以

uuid.uuid1(random_48_bits)

应该为您提供与 类似的结果uuid_generate_v1mc ,它是具有随机MAC地址的UUID1。

Should get you similar results as uuid_generate_v1mc, which is a UUID1 with a random MAC address.

要生成随机的48位,作为一个虚拟示例,我们可以使用:

To generate a random 48 bits, as a dummy example we can use:

import random
random_48_bits = random.randint(0, 2**48 - 1)

尝试一下:

>>> import uuid
>>> import random
>>> 2 ** 48 - 1
281474976710655
>>> uuid.uuid1(random.randint(0, 281474976710655))
UUID('c5ecbde1-cbf4-11e5-a759-6096cb89d9a5')

现在使用它做一个函数,并将其用作Django UUIDField 默认值 c $ c>

Now make a function out of it, and use it as the default for your Django UUIDField

自定义UUID,以及来自Instagram的示例

注意

Eg,提出您的自定义UUID方案并使用可用位对可能对您的应用程序有用的信息进行编码是完全可以的。您可以使用一些位来编码给定用户的国家/地区,使用一些位带有时间戳,使用一些位来随机性,等等。

E.g. you may use a few bits to encode the country of a given user, a few bits with a timestamp, some bits for randomness etc.

您可能想了解< a href = http://instagram-engineering.tumblr.com/post/10853187575/sharding-ids-at-instagram rel = noreferrer> Instagram(建立在Django和PostgreSQL上)制定了自己的UUID方案以提供帮助分片。

这篇关于Django UUIDField如何在Postgresql中生成UUID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆