Using a UUID as a primary key in Django models (generic relations impact)

前端 未结 6 728
旧巷少年郎
旧巷少年郎 2020-12-02 06:52

For a number of reasons^, I\'d like to use a UUID as a primary key in some of my Django models. If I do so, will I still be able to use outside apps like \"contrib.comments\

6条回答
  •  -上瘾入骨i
    2020-12-02 07:49

    As seen in the documentation, from Django 1.8 there is a built in UUID field. The performance differences when using a UUID vs integer are negligible.

    import uuid
    from django.db import models
    
    class MyUUIDModel(models.Model):
        id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    

    You can also check this answer for more information.

提交回复
热议问题