Q:

Pass South random unique default values when migrating

I am trying to forward migrate a model with existing data.  The model has a new field with constraints unique=True and null=False.  
When I do
./manage.py schemamigration myapp --auto

South lets me specify a default value for the new field by asking:
Specify a one-off value to use for existing columns now

Usually I set this to None but since this field needs to be unique I was wondering if it is possible to pass South a unique value via:
 >>> import uuid; uuid.uuid1().hex[0:35]

This gives me an error message 
!  Invalid input: invalid syntax 

Any ideas if it is possible to pass South random unique default values when migrating via the commandline? 
Thanks. 

A:

Unfortunately only the datetime module is available for use as a one-off value in a schemamigration. 
However, you can achieve the same effect by splitting this up into three migrations:

add new field to the model without constraints (with null=True, unique=False)
use a datamigration to add the UUID to the new field
add the constraint on the new field (with null=False, unique=True)

Tutorial on data migrations: http://south.readthedocs.org/en/0.7.6/tutorial/part3.html#data-migrations

A:

In django 1.7+ you can do the following.  It first adds the field with no indexing and no unique.  It then assigns the unique values (I based them on the name and used slugify method which you need to create) and finally alters the field again to add index and unique attributes. 
from django.db import migrations
import re
import django.contrib.postgres.fields
from common.utils import slugify
import django.core.validators

def set_slugs(apps, schema_editor):
    categories = apps.get_model("myapp", "Category").objects.all()
    for category in categories:
        category.slug = slugify(category.name)
        category.save()

class Migration(migrations. Migration):

    dependencies = [
        ('myapp', '0034_auto_20150906_1936'),
    ]

    operations = [
        migrations. AddField(
            model_name='category',
            name='slug',
            field=models. CharField(max_length=30, validators=[django.core.validators. MinLengthValidator(2), django.core.validators. RegexValidator(re.compile('^[0-9a-z-]+$'), 'Enter a valid slug.', ' invalid')], help_text='Required.  2 to 30 characters and can only contain a-z, 0-9, and the dash (-)', unique=False, db_index=False, null=True),
            preserve_default=False,
        ),
        migrations. RunPython(set_slugs),
        migrations. AlterField(
            model_name='category',
            name='slug',
            field=models. CharField(help_text='Required.  2 to 30 characters and can only contain a-z, 0-9, and the dash (-)', unique=True, max_length=30, db_index=True, validators=[django.core.validators. MinLengthValidator(2), django.core.validators. RegexValidator(re.compile('^[0-9a-z-]+$'), 'Enter a valid slug.', ' invalid')]),
        ),
    ]

