使用South从ImageField到ImageField的数据迁移 [英] Data Migration from ImageField to ImageField using South

查看:146
本文介绍了使用South从ImageField到ImageField的数据迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试几个小时,使用南方做最好的迁移,但不知何故,我一直在惨败。我正在尝试迁移到Sorl-Thumbnail。

I'm trying for several hours now to do the silliest migration using South, but somehow, I've been failing miserably.. I'm trying to migrate to Sorl-Thumbnail.

这是我的过渡模型:

class Deal(models.Model):
  image = ImageWithThumbsField(upload_to='deal_images',null=True,blank=True,sizes=(200,150),))
  new_image = ImageField(upload_to='new_deal_images',default='deal_images/thumb_deal_noimg.gif')

我现在迁移到目前为止:

And my foward migration it's this so far:

def forwards(self, orm):
  for deal in orm.Deal.objects.all():
    try:
      image_name = deal.image.name.split('/')[1]
      file_ = File(deal.image.open()) # I've also tried the method read()
      deal.new_image.save('new_deal_images/'+image_name,file_,save=False)
    except:
      deal.new_image = None  # For the default image kick in
    deal.save()

这是这段代码的最新版本。所有其他人,大多数都没有正确地将图像文件放在新目录中。

this is the most recent version of this code. All the others, mostly failed to put the image file in the new directory properly.

帮助...:)

时间过去....

好的...经过几次测试,我得到了这个代码:

Alright... After several tests i got this code:

 def forwards(self, orm):
     for deal in orm.Deal.objects.all():
        file_content = ContentFile(deal.image.read())
        deal.new_image.save(deal.image.name,file_content) *
        deal.save()

图像被复制并保存在新列(new_image)中,但事实是所有文件都保存在MEDIA_ROOT根目录中,而不是所需的子目录('new_deal_images )。我在*行中尝试过,但仍然没有运气:

The images are being copied and saved in the new column (new_image), but the thing is that all the files are being saved in the MEDIA_ROOT root, not in the desired sub-directory ('new_deal_images'). I tried this in the * line, but still no luck:

deal.new_image.save('new_ideal_images/'+deal.image.name,file_content)

没有工作...

请帮助...:)

Anothe time goes by ....

Anothe time goes by....

ok ...我认为南方有一些严重的问题:

ok... I think that there is some serious problem with South:

这段代码在Django Shell中完美地运行,将所有文件复制到正确的位置: / p>

This code works perfectly in the Django Shell, copying all the files to the correct place :

 15         for deal in Deal.objects.all():
 16             image_path = deal.image.path·
 17             file_ = File(open(image_path,'rb'))
 18             deal.new_image.save(deal.image.name,file_)
 19             deal.save()

但是,在Migration文件中的这个代码不会将MEDIA_ROOT根目录中的所有文件转储到正确的位置子目录:

But this code in the Migration file, does not, dumping all files in the MEDIA_ROOT root directory, without moving it to the correct subdirectory:

 15         for deal in orm.Deal.objects.all():
 16             image_path = deal.image.path·
 17             file_ = File(open(image_path,'rb'))
 18             deal.new_image.save(deal.image.name,file_)
 19             deal.save()


推荐答案

您可以在South orm中覆盖该字段的generate_filename方法。例如,这将将image字段中的所有图像复制到new_image字段,只需更改其存储的目录。

You can override the field's generate_filename method in the South orm. For example, this would copy all the images in the 'image' field over to the 'new_image' field, just changing the directory they're stored in.

for deal in orm.Deal.objects.all():
    deal._meta.get_field('new_image').generate_filename = \
        lambda inst, fn: os.path.join('new_deal_images', fn)
    img_data = SimpleUploadedfile(deal.image.name, deal.image.read())
    deal.image.close()
    setattr(deal, 'new_image', img_data)
    deal.save()

这篇关于使用South从ImageField到ImageField的数据迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆