Python 如何在 Django 中使用 UUID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32528224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use UUID in Django
提问by Chad Crowe
I am trying to get unique IDs for my Django objects. In Django 1.8 they have the UUIDField. I am unsure how to use this field in order to generate unique IDs for each object in my model.
我正在尝试为我的 Django 对象获取唯一 ID。在 Django 1.8 中,他们有 UUIDField。我不确定如何使用此字段为模型中的每个对象生成唯一 ID。
Here is what I have for the UUIDField
这是我对 UUIDField 的看法
import uuid
from django.db import models
class MyUUIDModel(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
class Person(models.Model):
...
unique_id = MyUUIDModel()
I can reproduce the id for the UUID model, but everytime I do I get the exact same id. :(. For Example:
我可以为 UUID 模型重现 id,但每次我都会得到完全相同的 id。:(。 例如:
person = Person.objects.get(some_field = some_thing)
id = person.unique_id.id
id then gives me the same id every time. What is wrong, how do I fix this? Thank you for your help!
id 然后每次都给我相同的 id。出了什么问题,我该如何解决?感谢您的帮助!
采纳答案by Alasdair
I'm not sure why you've created a UUID model. You can add the uuid field directly to the Person model.
我不确定您为什么创建了 UUID 模型。您可以将 uuid 字段直接添加到 Person 模型。
class Person(models.Model):
unique_id = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
Each person should then have a unique id. If you wanted the uuid to be the primary key, you would do:
每个人都应该有一个唯一的ID。如果您希望 uuid 作为主键,您可以这样做:
class Person(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
Your current code hasn't added a field to the person. It has created a MyUUIDModel
instance when you do MyUUIDModel(), and saved it as a class attribute. It doesn't make sense to do that, the MyUUIDModel
will be created each time the models.py loads. If you really wanted to use the MyUUIDModel, you could use a ForeignKey
. Then each person would link to a different MyUUIDModel instance.
您当前的代码尚未向此人添加字段。它MyUUIDModel
在您执行 MyUUIDModel() 时创建了一个实例,并将其保存为类属性。这样做没有意义,MyUUIDModel
每次加载models.py时都会创建。如果您真的想使用 MyUUIDModel,则可以使用ForeignKey
. 然后每个人将链接到不同的 MyUUIDModel 实例。
class Person(models.Model):
...
unique_id = models.ForeignKey(MyUUIDModel, unique=True)
However, as I said earlier, the easiest approach is to add the UUID field directly to the person.
但是,正如我之前所说,最简单的方法是将 UUID 字段直接添加到人员中。
回答by Rahul Gupta
You can directly add the id
field as a UUIDField
in the Person
model. There is no need for a separate MyUUIDModel
.
您可以直接添加id
字段作为UUIDField
在Person
模型。不需要单独的MyUUIDModel
.
I think you have confused it with the MyUUIDModel
used in the UUIDField examplewhere the id
is a UUIDField
. You can just use the below code and it will use UUIDs
for id
.
我想你已经与混淆它MyUUIDModel
在使用UUIDField例子,其中id
一个UUIDField
。您可以只使用下面的代码,它将UUIDs
用于id
.
import uuid
from django.db import models
class Person(models.Model):
...
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
回答by Sylvestre
You need to use the class you created as a subclass when declaring your Person model like this:
在像这样声明 Person 模型时,您需要使用您创建的类作为子类:
import uuid
from django.db import models
class MyUUIDModel(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
class Person(MyUUIDModel):
...
This way Person becomes a subclass of MyUUIDModel and will inherit its id field definition.
这样 Person 成为 MyUUIDModel 的子类,并将继承其 id 字段定义。
回答by Jatin Goyal
To use UUID in Django for a new modelsee Django Docs.
要在 Django 中为新模型使用 UUID,请参阅Django 文档。
However, if you want to use it for the existing model(with unique=True) having data corresponding to it, you will not be able to do it directly by the above documentation. It will create migration errors. To do it without losing the data follow all the steps carefully of this Django Documentation.
但是,如果要将其用于具有与其对应的数据的现有模型(具有unique=True),则无法通过上述文档直接执行此操作。它会产生迁移错误。要在不丢失数据的情况下执行此操作,请仔细遵循此Django 文档的所有步骤。