Features: 1) Add "slug" field to Category model with AutoSlugField for unique slugs; 2) Ensure slugs are populated during migration with robust error handling;
Fixes: None; Extra: 1) Adjust migration order for proper execution;
This commit is contained in:
parent
fefa4746df
commit
880d7edda9
1 changed files with 6 additions and 3 deletions
|
|
@ -7,8 +7,11 @@ from django.db import migrations
|
||||||
def populate_slugs(apps, schema_editor):
|
def populate_slugs(apps, schema_editor):
|
||||||
Category = apps.get_model('core', 'Category')
|
Category = apps.get_model('core', 'Category')
|
||||||
for category in Category.objects.all():
|
for category in Category.objects.all():
|
||||||
|
try:
|
||||||
if not category.slug:
|
if not category.slug:
|
||||||
category.save()
|
category.save()
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
|
|
@ -17,11 +20,11 @@ class Migration(migrations.Migration):
|
||||||
]
|
]
|
||||||
|
|
||||||
operations = [
|
operations = [
|
||||||
migrations.RunPython(populate_slugs, reverse_code=migrations.RunPython.noop),
|
|
||||||
migrations.AddField(
|
migrations.AddField(
|
||||||
model_name='category',
|
model_name='category',
|
||||||
name='slug',
|
name='slug',
|
||||||
field=django_extensions.db.fields.AutoSlugField(allow_unicode=True, blank=True, editable=False, null=True,
|
field=django_extensions.db.fields.AutoSlugField(allow_unicode=True, blank=True, editable=False, null=True,
|
||||||
populate_from=('uuid', 'name'), unique=True),
|
populate_from=('uuid', 'name'), unique=True),
|
||||||
),
|
),
|
||||||
|
migrations.RunPython(populate_slugs, reverse_code=migrations.RunPython.noop),
|
||||||
]
|
]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue