Django & South: Custom field methods are not executed when doing obj.save() in a data migration Django & South: Custom field methods are not executed when doing obj.save() in a data migration django django

Django & South: Custom field methods are not executed when doing obj.save() in a data migration


Generally you should explicitly replicate the code that generates the field's contents as closely as possible in the migration (A rare example of purposeful code duplication). The code in your approach, even if it worked, would call pre_save as defined at the time of executing the migration, which may have changed or even fail with the models state at the time the migration was created (It may depend on other fields not being present at an earlier time etc.).

So the correct approach in you example would be to use slugify() directly, as it is done in the SlugField's pre_save method:

from django.template.defaultfilters import slugifyclass Migration(DataMigration):    def forwards(self, orm):        "Write your forwards methods here."        for myobj in orm['myapp.MyModel'].objects.all():            myobj.slug = slugify(myobj.otherfield)            myobj.save()


I solved this temporarily by obtaining the model field instance and calling it's pre_save directly:

class Migration(DataMigration):    def forwards(self, orm):        "Write your forwards methods here."        # Note: Remember to use orm['appname.ModelName'] rather than "from appname.models..."        for myobj in orm['myapp.MyModel'].objects.all():            slug_field = myobj._meta.get_field_by_name('slug')[0]            myobj.slug = slug_field.pre_save(myobj, add=False)            myobj.save()

However it feels cumbersome to take this into account for custom fields...