You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
padelclub_backend/tournaments/signals.py

18 lines
604 B

import random
import string
from django.db.models.signals import post_save
from django.dispatch import receiver
from .models import Club
def generate_unique_code():
characters = string.ascii_letters + string.digits
while True:
code = ''.join(random.sample(characters, 3))
if not Club.objects.filter(broadcast_code=code).exists():
return code
@receiver(post_save, sender=Club)
def assign_unique_code(sender, instance, created, **kwargs):
if created and not instance.broadcast_code:
instance.broadcast_code = generate_unique_code()
instance.save()