from django.db import models
from django.utils.text import slugify

# Model files of Category.
class category(models.Model):
    title = models.CharField(max_length=120, primary_key=True)
    image = models.FileField(null=True, blank=True)
    slug = models.SlugField(unique=True, editable=False, max_length=130)

    def __str__(self):
        return self.title

    # The slug function used to create a link.

    def get_unique_slug(self):
        slug = slugify(self.title)
        unique_slug = slug
        counter = 1

        while category.objects.filter(slug=unique_slug).exists():
            unique_slug = slug + "-" + str(counter)
            counter += 1
        return unique_slug

    def save(self, *args, **kwargs):
        self.slug = self.get_unique_slug()

        return super(category, self).save(*args, **kwargs)

from django.db import models
from django.utils.text import slugify

# Model files of Category.
class category(models.Model):
    title = models.CharField(max_length=120, primary_key=True)
    image = models.FileField(null=True, blank=True)
    slug = models.SlugField(unique=True, editable=False, max_length=130)

    def __str__(self):
        return self.title

    # The slug function used to create a link.

    def get_unique_slug(self):
        slug = slugify(self.title)
        unique_slug = slug
        counter = 1

        while category.objects.filter(slug=unique_slug).exists():
            unique_slug = slug + "-" + str(counter)
            counter += 1
        return unique_slug

    def save(self, *args, **kwargs):
        self.slug = self.get_unique_slug()

        return super(category, self).save(*args, **kwargs)

# Model files of Category. # The slug function used to create a link.