How to avoid IntegrityError and db collision when saving model after sanitize slug with slugify in Django?

I am currently working on a simple web project, where I have several models that make use of SlugField as URL, such as Page, Note and Tag. From the admin panel the user can edit the slug and other fields of the model.

The user can modify the slug manually to make it more user friendly.

The user can also enter content in uppercase, and special characters, either by mistake or by malpractice.

The validation of the slug as a unique field is done correctly in the frontend if it is entered correctly, but not if uppercase characters or special characters are entered.

Here is an example:

User creates a new tag with slug: new-tag Hit save, and it does the work.

User creates a new tag with slug new-tag Hit save, and the user get a front end validation warning, saying is not possible. Ok, as expected.

User creates a new tag with slug: New-Tag(noticed the capital letters) Hit save and we got.

  • Exception Type: IntegrityError
  • Exception Value: UNIQUE constraint failed: notes_tag.slug Which is expected and im trying to address it.

This is my model: You can check full project here: Github repo


class Tag(models.Model):
    id = models.UUIDField(default=uuid.uuid4, unique=True,
                          primary_key=True, editable=False)
    name = models.CharField(max_length=255)
    slug = models.SlugField(unique=True)

    def save(self, *args, **kwargs):
        # todo: check colision with others URLs
        self.slug = slugify(self.slug)
        super(Tag, self).save(*args, **kwargs)

My first approach to solve the colision:

def save(self, *args, **kwargs):
        self.slug = slugify(self.slug)
        original_slug = self.slug
        queryset = Tag.objects.filter(slug=self.slug).exists()
        counter = 1
        while queryset:
            self.slug = f"{original_slug}-{counter}"
            counter += 1
            queryset = Tag.objects.filter(slug=self.slug).exists()
        super(Tag, self).save(*args, **kwargs)

For me it doesnt seem like they way it should be , to check the database with a while loop cool be dangerous. But ok, i didt like this and the problem is this save method is executing always, with new items creation and with updates.

I try to use if self.id to check if is new item or just updated but as my Model override the id field, by the time the execution reach the save method the item as already been created and got an id.

So, any recommendation on that issue? Any way to follow?

My guess is that might be a simplier solution to validate the slug after slugify and use the same front end warning is built in with django.

But im wonder why self.id exist once the execution reach the save method...?

I already check:

Thanks in advance.

I try to check if entity already exist in database, then add a '-number' at the end of the slug, but cannot check whether a item is create or update so the addition of the '-number' is happening evertyhing after i hit save.

best solution is check slug in form.clean(). or before create object.

but, if you want check slug in Tag.save()...:

class Tag(models.Model):
    ...
    def save(self, *args, **kwargs):
        self.slug = slugify(self.slug)
        if self.__class__.objects.filter(slug=self.slug).count():
           # do error process
        else: retrun super(T).save(*args, **kwargs)
        return
Back to Top