Jak wygenerować HASH dla modelu Django
Próbuję wygenerować unikalne wartości HASH dla moich modeli Django o 10 cyfrach wypróbowałem te metody, ale otrzymuję ten błąd
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: column hash_3 is not unique
Oto co próbowałem:
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)
Próbowałem również tej metody:
def _createHash():
"""This function generate 10 character long hash"""
return hexlify(os.urandom(5))
Mam skrypt, który wstawia dane do tego modelu za każdym razem, gdy uruchamiam mój skrypt, otrzymałem wyżej wymieniony błąd .. Czy jest jakiś inny sposób na zrobienie tego .. Chcę przechowywać unikalne wartości skrótu w kolumnachhash_1,hash_2,hash_3
.