Django provides the slugify function in the django.utils.text module, which can be used to customize slug generation. Here's an example:
from django.db import models
from django.utils.text import slugify
class MyModel(models.Model):
title = models.CharField(max_length=100)
slug = models.SlugField(unique=True)
def save(self, *args, **kwargs):
self.slug = slugify(self.title) # Using slugify for custom slug generation
super().save(*args, **kwargs)
In this example, slugify is used to convert the title field to a slug by removing any special characters and replacing spaces with hyphens.