0
votes
 class AddCategory(admin.ModelAdmin):
   list_display = ['name', 'slug']  
   prepopulated_fields = {'slug': ('name',)}

class CategorySerializer(serializers.ModelSerializer): class Meta: model = Category fields = ['name', 'slug', 'icon'] lookup_field = 'slug'

2

2 Answers

0
votes

You can make it in save method inside model manually

  1. create custom_slugify method:

    def custom_slugify(slug):
      slug = slug.replace(" ", "-")
      slug = slug.replace(",", "-")
      slug = slug.replace("(", "-")
      slug = slug.replace(")", "")
      slug = slug.replace("؟", "")
      return slug
    
  2. call it into save method

     def save(self, *args, **kwargs):
       self.slug = custom_slugify(self.name)
       super().save(*args, **kwargs)
    
0
votes
from django.utils.text import slugify

class Category(models.Model):
    name = models.CharField(max_length=100)
    slug = models.SlugField(max_length=120)

    def save(self, *args, **kwargs):
        if not self.pk:
            self.slug = slugify(self.name)
        super().save(*args, **kwargs)

Override your save method to generate your slug automatically.