Search code examples
pythondjangopython-imaging-librarysorl-thumbnaildjango-migrations

Migration issue with custom model field


Ï'm upgrading my site to Django 1.7.1 - especially to get the neat migration feature. However, I am running into some issues regarding migrations and a custom model field.

The most annoying thing about this is, that the traceback doesn't give me that much information - here it is (removed some paths for security reasons):

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

The custom file field is a ImageWithThumbnailField from "the old" sorl-thumbnail package. It uses PIL 1.1.7.

I have no issues with the field itself except for this migration bug.a

Was wondering if any of you have encountered the same error, and found a solution?


Solution

  • For anyone encountering this issue, here is the solution:

    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