im using uuid token to activate users account , so every client created have their own code in the Client model , but when i create new client it generate always the same Token as others , i have used the uuid 4 , is their anyother ways to generate unique client code to activate their account or some other method then uuid , or just im missing a point on how to generate uuid on models , their is my model and client creation codes :
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)
You need to create a custom save
method and assign this there.
def save(self, *args, **kwargs):
self.token = generateUUID()
super(client, self).save(*args, **kwargs)