我正在尝试为我的 10 位 Django 模型生成唯一的 HASH 值我已经尝试过这些方法,但我收到了这个错误
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: column hash_3 is not unique
这是我尝试过的:
import os
import time
import hashlib
from os import path
from binascii import hexlify
from django.db import models
from django.contrib import admin
from django.core.files.storage import FileSystemStorage
#------------------------------------------------------------------------------
def _createHash():
"""This function generate 10 character long hash"""
hash = hashlib.sha1()
hash.update(str(time.time()))
return hash.hexdigest()[:-10]
class tags(models.Model):
""" This is the tag model """
seo_url1 = models.URLField()
seo_url2 = models.URLField()
seo_url3 = models.URLField()
tagDescription = models.TextField() # Tag Description
tag = models.CharField(max_length=200) # Tag name
tagSlug = models.CharField(max_length=400) # Extra info can be added to the existing tag using this field
updatedAt = models.DateTimeField(auto_now=True) # Time at which tag is updated
createdAt = models.DateTimeField(auto_now_add=True) # Time at which tag is created
hash_1 = models.CharField(max_length=10,default=_createHash(),unique=True)
hash_2 = models.CharField(max_length=10,default=_createHash(),unique=True)
hash_3 = models.CharField(max_length=10,default=_createHash(),unique=True)
我也试过这个方法:
def _createHash():
"""This function generate 10 character long hash"""
return hexlify(os.urandom(5))
我有一个脚本,每次我运行我的脚本时都会将数据插入到这个模型中,我得到上面提到的错误..有没有其他方法可以做到这一点..我想将唯一的哈希值存储到列hash_1,hash_2,hash_3
中。