2

I am performing a data migration, moving images from inside one model out to their own model.

class OldCrappyModel(models.Model):
    ...
    original_image = models.ImageField(upload_to=upload_original_image, null=True, blank=True)
    my_events_image = models.ImageField(upload_to=upload_my_promotions_image, null=True, blank=True)
    ... 

class MyImage(models.Model):
    original_image = models.ImageField(upload_to=upload_original_image, null=True, blank=True)
    my_events_image = models.ImageField(upload_to=upload_my_promotions_image, null=True, blank=True)
    ...

The upload_to methods just return a unique name for the file.

When I go to migrate them (in a South datamigration):

i = MyImage(
    my_events_image = old.my_events_image,
    original_image = old.original_image,
)
i.save()

The problem is, we have some images that are not in the same place as the new ones. When the copy happens, it translates the url from the old ImageField into a url that would work for the new one. For example:

old.url comes out to path/to/something/awesome.jpg

i.url becomes new/media/root/awesome.jpg

How can I preserve the ImageField without anything happening to it when it saves? I'd like to avoid having to make new copies of all the files if possible.

raphael
  • 447
  • 4
  • 17

1 Answers1

0

Disable upload_to for the duration of the migration. Set the upload_to manually while copying instances.

After the migration, enable it back.