In my Django model I have two fields, name
(a regular CharField
) and slug
, a custom field that generates the slug based on a field name passed in the field definition, in this case I use name
for this.
First, the model only had the name
field, with it's corresponding migrations and all. Then I needed to add the slug
field, so following South conventions, I added the slug field with unique=False
, create the schema migration, then created a data migration, set the unique=True
and create another migration for this last change.
Since the value of the slug is generated on model save, in the forwards
method of the data migration what I did was to iterate over the queryset returned by orm['myapp.MyModel'].objects.all()
and calling the save()
method on each instance.
But the value of the slug field is never generated. Under an IPython session I did the same thing, but referencing the model as from myapp.models import MyModel
, and worked. Using some debug statements, printing the type
of the model returned by South's orm dict shows the real class, it doesn't appear to be an fake model constructed on the fly by South.
The slug field creates it's value when the pre_save
method. How to force it to be called during a data migration? I need to ensure the uniqueness of the value so when the index is applied in a later schema migration, the columns doesn't contains duplicate values.
Thanks!
BTW: The slug field class does define the south_field_triple
so South plays nice with it.
EDIT: Please see my answer. But more like an answer, it feels more like a hack. Is there a better/right way to do this?