使用 uuid 的唯一令牌生成相同的代码
Unique token using uuid is generating same codes
我使用 uuid 令牌激活用户帐户,因此创建的每个客户端在客户端模型中都有自己的代码,但是当我创建新客户端时,它总是生成与其他客户端相同的令牌,我使用了 uuid 4 ,是他们用其他任何方法生成唯一的客户代码来激活他们的帐户或其他一些方法,然后是 uuid,或者我只是缺少关于如何在模型上生成 uuid 的要点,
他们是我的模型和客户创建代码:
Model.py :
def generateUUID():
return str(uuid4())
class client(models.Model):
name = models.CharField(max_length=45, unique=True)
email = models.EmailField(max_length=85)
date_of_birth = models.DateField(null=True, blank=True)
height = models.IntegerField(null=True, blank=True)
weight = models.IntegerField(null=True , blank=True)
created_at = models.DateTimeField(auto_now_add=True)
picture = models.URLField(null=True ,blank=True)
affiliation = models.ForeignKey(User, on_delete=models.CASCADE, related_name="client_coach")
reference = models.OneToOneField(User, on_delete=models.CASCADE, null=True , blank=True)
is_active = models.BooleanField(default=False)
token = models.CharField( max_length=85, default=generateUUID())
def __str__(self):
return self.name
def getname(self):
return self.coach.name
view.py:
if request.method == 'POST' and request.POST['action'] == 'client':
form = ClientForm(request.POST)
print(form)
name = request.POST.get('name')
email = request.POST.get('email')
#client_code = code.objects.create()
new_client = client.objects.create(name = name, email = email, affiliation = request.user)
new_client.save()
return JsonResponse({'client': model_to_dict(new_client)}, status=200)
您需要创建自定义 save
方法并将其分配到那里。
def save(self, *args, **kwargs):
self.token = generateUUID()
super(client, self).save(*args, **kwargs)
我使用 uuid 令牌激活用户帐户,因此创建的每个客户端在客户端模型中都有自己的代码,但是当我创建新客户端时,它总是生成与其他客户端相同的令牌,我使用了 uuid 4 ,是他们用其他任何方法生成唯一的客户代码来激活他们的帐户或其他一些方法,然后是 uuid,或者我只是缺少关于如何在模型上生成 uuid 的要点, 他们是我的模型和客户创建代码:
Model.py :
def generateUUID():
return str(uuid4())
class client(models.Model):
name = models.CharField(max_length=45, unique=True)
email = models.EmailField(max_length=85)
date_of_birth = models.DateField(null=True, blank=True)
height = models.IntegerField(null=True, blank=True)
weight = models.IntegerField(null=True , blank=True)
created_at = models.DateTimeField(auto_now_add=True)
picture = models.URLField(null=True ,blank=True)
affiliation = models.ForeignKey(User, on_delete=models.CASCADE, related_name="client_coach")
reference = models.OneToOneField(User, on_delete=models.CASCADE, null=True , blank=True)
is_active = models.BooleanField(default=False)
token = models.CharField( max_length=85, default=generateUUID())
def __str__(self):
return self.name
def getname(self):
return self.coach.name
view.py:
if request.method == 'POST' and request.POST['action'] == 'client':
form = ClientForm(request.POST)
print(form)
name = request.POST.get('name')
email = request.POST.get('email')
#client_code = code.objects.create()
new_client = client.objects.create(name = name, email = email, affiliation = request.user)
new_client.save()
return JsonResponse({'client': model_to_dict(new_client)}, status=200)
您需要创建自定义 save
方法并将其分配到那里。
def save(self, *args, **kwargs):
self.token = generateUUID()
super(client, self).save(*args, **kwargs)