from django.db import migrations from django.utils.text import slugify def gen_slugs(apps, schema_editor): MyModel = apps.get_model('hadis', 'HadisCategory') # Replace with your app/model name for row in MyModel.objects.all(): # 1. Basic slugify base_slug = slugify(row.title) # Assuming you slugify the 'name' field slug = base_slug n = 1 # 2. Ensure uniqueness (if two rows have the same name) while MyModel.objects.filter(slug=slug).exists(): slug = f"{base_slug}-{n}" n += 1 row.slug = slug row.save() class Migration(migrations.Migration): dependencies = [ ('hadis', '0006_hadiscategory_slug'), ] operations = [ # Call the function migrations.RunPython(gen_slugs, reverse_code=migrations.RunPython.noop), ]