我正在尝试为我的Django模型生成10位唯一的哈希值,我尝试了这些方法,但出现了错误。
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
中。
_createHash()
,我该怎么做呢? - Vaibhav Jaindef _generateSEOURL(pattern,StringToconcatenate): return pattern + StringToconcatenate
,我想传递seo_url1 = models.URLField(default=_generateSEOURL("discounts-in-",tag))
这样的参数...来将一些字符串连接到每个标签值上,那么我该怎么做呢? - Vaibhav Jaindef _createHashForModel(): return _createHash(10)
然后default=_createHashForModel
- ilovett