我编写了一个迁移,它将ImageField
的子类添加到现有模型中。作为迁移的一部分,我想将另一个图像场的内容保存到该图像场。
以下是迁移:
from __future__ import unicode_literals
import os
from django.db import models, migrations
import search.models
import imagekit.models.fields
def populate_image_thumb(apps, schema_editor):
ProductImage = apps.get_model('search', 'ProductImage')
for o in ProductImage.objects.all():
filename = os.path.basename(o.image.file.name)
o.image_thumb.save(filename, o.image)
class Migration(migrations.Migration):
dependencies = [
('search', '0015_auto_20150811_1516'),
]
operations = [
migrations.AddField(
model_name='productimage',
name='image_thumb',
field=imagekit.models.fields.ProcessedImageField(upload_to=search.models.product_thumb_upload_location, default=2),
preserve_default=False,
),
migrations.RunPython(populate_image_thumb),
]
追溯:
Operations to perform:
Synchronize unmigrated apps: staticfiles, tsl_auth, messages, allauth, haystack, project_core, rest_framework, mptt
Apply all migrations: feed, account, search, sessions, social, auth, discussions, sites, actstream, conversations, contenttypes, socialaccount, admin
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying search.0016_productimage_image_thumb...Traceback (most recent call last):
File "/Users/sampeka/virtualenv/thestylelocker/lib/python3.4/site-packages/django/core/management/base.py", line 444, in execute
output = self.handle(*args, **options)
File "/Users/sampeka/virtualenv/thestylelocker/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 221, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/Users/sampeka/virtualenv/thestylelocker/lib/python3.4/site-packages/django/db/migrations/executor.py", line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
File "/Users/sampeka/virtualenv/thestylelocker/lib/python3.4/site-packages/django/db/migrations/executor.py", line 148, in apply_migration
state = migration.apply(state, schema_editor)
File "/Users/sampeka/virtualenv/thestylelocker/lib/python3.4/site-packages/django/db/migrations/migration.py", line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Users/sampeka/virtualenv/thestylelocker/lib/python3.4/site-packages/django/db/migrations/operations/special.py", line 183, in database_forwards
self.code(from_state.apps, schema_editor)
File "/Users/sampeka/dev/thestylelocker/search/migrations/0016_productimage_image_thumb.py", line 14, in populate_image_thumb
o.image_thumb.save(filename, o.image)
File "/Users/sampeka/virtualenv/thestylelocker/lib/python3.4/site-packages/imagekit/models/fields/files.py", line 12, in save
content = generate(spec)
File "/Users/sampeka/virtualenv/thestylelocker/lib/python3.4/site-packages/imagekit/utils.py", line 139, in generate
f = NamedTemporaryFile()
File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/tempfile.py", line 460, in NamedTemporaryFile
(fd, name) = _mkstemp_inner(dir, prefix, suffix, flags)
File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/tempfile.py", line 200, in _mkstemp_inner
fd = _os.open(file, flags, 0o600)
OSError: [Errno 24] Too many open files: '/var/folders/pd/sj03p3zn5cd201ghz54frv880000gn/T/tmpqoa4yecp'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/sampeka/virtualenv/thestylelocker/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/Users/sampeka/virtualenv/thestylelocker/lib/python3.4/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/sampeka/virtualenv/thestylelocker/lib/python3.4/site-packages/django/core/management/base.py", line 393, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/sampeka/virtualenv/thestylelocker/lib/python3.4/site-packages/django/core/management/base.py", line 458, in execute
translation.activate(saved_locale)
File "/Users/sampeka/virtualenv/thestylelocker/lib/python3.4/site-packages/django/utils/translation/__init__.py", line 146, in activate
return _trans.activate(language)
File "/Users/sampeka/virtualenv/thestylelocker/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 222, in activate
_active.value = translation(language)
File "/Users/sampeka/virtualenv/thestylelocker/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 206, in translation
_translations[language] = DjangoTranslation(language)
File "/Users/sampeka/virtualenv/thestylelocker/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 115, in __init__
self._init_translation_catalog()
File "/Users/sampeka/virtualenv/thestylelocker/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 153, in _init_translation_catalog
translation = self._new_gnu_trans(localedir, use_null_fallback)
File "/Users/sampeka/virtualenv/thestylelocker/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 136, in _new_gnu_trans
fallback=use_null_fallback)
File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/gettext.py", line 409, in translation
with open(mofile, 'rb') as fp:
OSError: [Errno 24] Too many open files: '/Users/sampeka/virtualenv/thestylelocker/lib/python3.4/site-packages/django/conf/locale/en/LC_MESSAGES/django.mo'
我没有在我的代码中处理打开或关闭文件,但我应该这样做吗?我是否需要以某种方式控制这个过程?
修改1
在尝试将图像写入下一个字段之前,我还尝试了以下方法将图像物理加载到内存中。我仍然被抛出同样的错误:
def populate_image_thumb(apps, schema_editor):
ProductImage = apps.get_model('search', 'ProductImage')
for o in ProductImage.objects.all():
filename = o.image.file.name
fileio = io.BytesIO()
with open(filename, 'rb') as open_file:
fileio.write(open_file.read())
image = InMemoryUploadedFile(file=fileio, field_name=None,
name=filename, content_type='image/%s' % filename.split('.')[-1],
size=fileio.__sizeof__(), charset=None)
o.image_thumb.save(filename, image)
答案 0 :(得分:2)
FileField
有close()
方法,您应该在保存图片后尝试调用它:o.image_thumb.close()
您在Managing files部分的Django文档中提到了错误:
在访问文件字段时,关闭文件尤其重要 循环遍历大量对象。如果没有手动关闭文件 访问它们之后,可能会出现文件描述符用完的风险 出现。这可能会导致以下错误:
IOError:[Errno 24]打开文件太多