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):
|
||||
Category = apps.get_model('core', 'Category')
|
||||
for category in Category.objects.all():
|
||||
if not category.slug:
|
||||
category.save()
|
||||
try:
|
||||
if not category.slug:
|
||||
category.save()
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
|
@ -17,11 +20,11 @@ class Migration(migrations.Migration):
|
|||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(populate_slugs, reverse_code=migrations.RunPython.noop),
|
||||
migrations.AddField(
|
||||
model_name='category',
|
||||
name='slug',
|
||||
field=django_extensions.db.fields.AutoSlugField(allow_unicode=True, blank=True, editable=False, null=True,
|
||||
populate_from=('uuid', 'name'), unique=True),
|
||||
),
|
||||
migrations.RunPython(populate_slugs, reverse_code=migrations.RunPython.noop),
|
||||
]
|
||||
|
|
|
|||
Loading…
Reference in a new issue