Boxtitoli_Django/titoli/signals.py

12 lines
385 B
Python

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.slug_id:
random_string = generate_random_string()
instance.slug_id = random_string