How to ensure that UUID slug will be always unique?

Is this a correct way to ensure that slug will be always unique? Any potential issues?

import uuid

class Item(models.Model):
    name = models.CharField(max_length=255)
    slug = models.SlugField(max_length=150, unique=True)

    def save(self, **kwargs):
        if not self.id:
            self.slug = uuid.uuid4()
        super().save(**kwargs)

This doesn’t guarantee that slug will always be unique. However, under the assumption that you’re not trying to cause problems, the probability that a duplicate UUID will be generated is sufficiently close to zero that I wouldn’t worry about it at all.

Ken

You are right, but in different models it is not possible to show a duplicate value