0

Ï'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?

rulzart
  • 103
  • 1
  • 9
  • I assume the problem is that 'old' sorl-thumbnail doesn't provide support for Django 1.7 migrations, which basically means a `deconstruct` method on the field class https://docs.djangoproject.com/en/1.7/topics/migrations/#adding-a-deconstruct-method ...you could subclass their field and add it yourself, it's almost the same as the `south_field_triple` method used for South – Anentropic Dec 05 '14 at 14:17
  • Yes, I've tried adding the deconstructor class to some of the fields, but no luck. Will add to the rest and see if it works. – rulzart Dec 05 '14 at 14:22
  • Do you have any idea how the deconstructor def would look? – rulzart Dec 05 '14 at 14:25
  • I found the answer, see below. Thanks for the help! – rulzart Dec 05 '14 at 14:33
  • Updated link to Django docs for custom model field deconstruction https://docs.djangoproject.com/en/4.2/howto/custom-model-fields/#field-deconstruction – Esteban Jun 22 '23 at 22:17

1 Answers1

2

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
kjagiello
  • 8,269
  • 2
  • 31
  • 47
rulzart
  • 103
  • 1
  • 9