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

前端 未结 6 743
旧巷少年郎
旧巷少年郎 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条回答
  •  孤城傲影
    2020-12-02 07:33

    The question can be rephrased as "is there a way to get Django to use a UUID for all database ids in all tables instead of an auto-incremented integer?".

    Sure, I can do:

    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    

    in all of my tables, but I can't find a way to do this for:

    1. 3rd party modules
    2. Django generated ManyToMany tables

    So, this appears to be a missing Django feature.

提交回复
热议问题