Search code examples
djangodjango-modelsdjango-validation

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


The questions i try to answer here:

  • how can i check inside the save method if a item is new created or updated?
  • is there any posible way to create a validator which runs in the middle of the fron-end and the model?

background

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.

after edit with my solution to this issue

Good everyone, I answer myself how I solved the problem.

What I really wanted to achieve was that the user could edit slug in admin panel and validate that this slug is unique before save to the db.

Currently my model uses SlugField which does a default validation of the slug type, but django's default validation does not use Slugify. Also, django's default slugield validation allows the creation of case-sensitive URLs, so two urls in uppercase and lowercase, even if they are the same in content, are considered different.

In my model, when overwriting the save method, it overrides the slug transformation with slugify, which returns everything in lowercase.

In this way, given two urls: url-a/ and another /URL-a/, when creating the second one, it passed the django validation for being considered as two different urls, but in my save method, the two were transformed into the same one colliding in the database.

SOLUTION:

Use a validator for the slugfield.

This is my new slugfield in the model:

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,validators=[validate_slugify])

    def save(self, *args, **kwargs):
        self.slug = slugify(self.slug)
        super(Tag, self).save(*args, **kwargs)

And here validate_slugify from app.validators.py

def validate_slugify(value):
    # if slugify version is different to value, there are something wrong, so raise error.
    if slugify(value) != value:
        raise  ValidationError("Please make sure slug is in lowercase")

This solves my original problem and does not allow the creation of the URL if it is going to collide, throwing a visible error in the user administration panel.


Solution

  • 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().save(*args, **kwargs)
            return