from django.db.models.signals import pre_save from django.dispatch import receiver from random_id.utils import generate_random_string from .models import Titolo @receiver(pre_save, sender=Titolo) def add_slug_to_id(sender, instance, *args, **kwargs): if instance and not instance.id: random_string = generate_random_string() instance.id = random_string