自定义模型字段的迁移问题

时间:2014-12-05 12:58:42

标签: python django python-imaging-library sorl-thumbnail django-migrations

我将我的网站升级到Django 1.7.1 - 特别是为了获得整洁的迁移功能。 但是,我遇到了一些有关迁移和自定义模型字段的问题。

关于这一点最令人烦恼的是,回溯并没有给我那么多信息 - 这里是(出于安全原因删除了一些路径):

Traceback (most recent call last):
    File "PyCharm 3.4.1\helpers\pycharm\django_manage.py", line 23, in <module>
        run_module(manage_file, None, '__main__', True)
    File "C:\Python27\Lib\runpy.py", line 176, in run_module
        fname, loader, pkg_name)
    File "C:\Python27\Lib\runpy.py", line 82, in _run_module_code
        mod_name, mod_fname, mod_loader, pkg_name)
    File "C:\Python27\Lib\runpy.py", line 72, in _run_code
        exec code in run_globals
    File "portal\manage.py", line 10, in <module>
        execute_from_command_line(sys.argv)
    File "lib\site-packages\django\core\management\__init__.py", line 385, in execute_from_command_line
        utility.execute()
    File "lib\site-packages\django\core\management\__init__.py", line 377, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
    File "lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
        self.execute(*args, **options.__dict__)
    File "lib\site-packages\django\core\management\base.py", line 338, in execute
        output = self.handle(*args, **options)
     File "lib\site-packages\django\core\management\commands\makemigrations.py", line 90, in handle
        ProjectState.from_apps(apps),
     File "lib\site-packages\django\db\migrations\state.py", line 104, in from_apps
        model_state = ModelState.from_model(model)
     File "lib\site-packages\django\db\migrations\state.py", line 182, in from_model
e,
TypeError: Couldn't reconstruct field image on core.GalleryImage: argument of type 'NoneType' is not iterable

自定义文件字段是来自“旧”sorl-thumbnail包的ImageWithThumbnailField。它使用PIL 1.1.7。

我对该字段本身没有任何问题,除了这个迁移bug.a

想知道你们是否遇到过同样的错误,并找到了解决办法吗?

1 个答案:

答案 0 :(得分:2)

对于遇到此问题的任何人,以下是解决方案:

def deconstruct(self):
    # Use ImageField as path, as the deconstruct() will return ImageWithThumbnailsField
    field_class = "django.db.models.fields.files.ImageField"
    name, path, args, kwargs = super(BaseThumbnailField, self).deconstruct()
    return name, field_class, args, kwargs