使用 South 将数据从 ImageField 迁移到 ImageField
我现在正在尝试使用 South 进行几个小时的最愚蠢的迁移,但不知何故,我一直失败得很惨。我正在尝试迁移到 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')
到目前为止,我的向前迁移是这样的:
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()
这是此代码的最新版本。所有其他人大多未能将图像文件正确地放入新目录中。
帮助...:)
时间流逝....
好吧...经过几次测试,我得到了这个代码:
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')中。我在 * 行中尝试过这个,但仍然没有运气:
deal.new_image.save('new_ideal_images/'+deal.image.name,file_content)
Sill 无法工作...
请帮忙...:)
又过了一段时间....
好吧...我认为 South 有一些严重的问题:
这代码在 Django Shell 中完美运行,将所有文件复制到正确的位置:
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()
但是迁移文件中的这段代码却不能,转储 MEDIA_ROOT 根目录中的所有文件,而不将其移动到正确的子目录:
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()
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.
Here is my transitional model:
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.
Help... :)
Time goes by....
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()
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)
Sill not working...
Please help... :)
Anothe time goes by....
ok... I think that there is some serious problem with South:
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()
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在 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.
我遇到了同样的问题,并通过直接分配路径而不是文件来解决它:
I had the same problem, and worked around it by assigning a path directly, instead of a file:
感谢 Mike Fogel 的回答,我通过将图像从一个模型移动到另一个模型的迁移解决了这个问题:
Thanks to Mike Fogel's answer I solve this problem in a migrations that moves images from one model into another:
使用 South 将 ImageField 从一个存储迁移到不同的存储
虽然这可能无法直接回答您的问题,但我想我应该为 django South imageField 迁移主题添加一些可能有用的见解(该问题目前是网络上最好的资源)为了)。
South 在数据迁移中处理图像字段的一个重要问题是,作为其 ORM 冻结的副作用,它似乎假设将使用 DefaultStorage,因为存储不会被冻结。因此,如果您为现场使用了自定义存储,那么事情可能不会按预期工作。但 @cberner 提供了一些很好的见解,您可以直接利用存储来保存和操作文件,然后只需设置该字段的路径并保存它。借鉴他的回答,我想出了以下数据迁移,将我的所有现有图像从一个存储(默认,文件系统存储)迁移到一个新存储(AWS):
这是从默认存储到新存储的迁移(运行同时将新存储添加到字段定义中):(
作为参考,这是我的自定义存储,其存储桶与默认 AWS 存储桶不同,该存储桶已被我的静态文件存储使用)
希望这能帮助一些迷失的灵魂。
Migrating an ImageField from One Storage to a Different Storage with South
Although this might not directly answer your question, I figured I'd add some potentially helpful insight to the django south imageField migration topic (which this question is currently the best resource on the web for).
One significant problem with South's handling of image fields in data migrations is that, as a side effect of its ORM-Freezing, it seems to assume the DefaultStorage will be used, since the storage doesn't get frozen. So if you've got a custom storage for your field things probably won't work as expected. But @cberner provides some great insight in that you can save and manipulate the files directly by utilizing the storage directly and then simply set the path to the field and save it. Piggybacking off of his answer, I came up with the following datamigration to migrate all of my existing images from one storage (default, filesystem storage) to a new storage (AWS):
here's the migration from the default storage to the new storage (run simultaneously with adding the new storage to the field definition):
(For reference, here's my custom storage with a different bucket than the default AWS bucket, which is already used by my static files storage)
Hopefully that will help some lost soul.