Generate unique hashes for django models

后端 未结 4 1397
感情败类
感情败类 2021-02-01 07:20

I want to use unique hashes for each model rather than ids.

I implemented the following function to use it across the board easily.

import random,hashlib         


        
4条回答
  •  广开言路
    2021-02-01 07:49

    Django 1.8+ has a built-in UUIDField. Here's the suggested implementation, using the standard library's uuid module, from the docs:

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

    For older django versions you can use the django-uuidfield package.

提交回复
热议问题