﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
22436	migrations fail on custom upload_to on ImageField	David Binetti <dbinetti@…>	Andrew Godwin	"One of my models has an ImageField with a dynamically created upload_to:

{{{
class Photo(models.Model):


    account = models.ForeignKey(Account)
    
    ...    

    def upload_thumb(instance, filename):
        return 'photos/{0}/dropbox{1}'.format(instance.account.id, filename)

    thumbnail = models.ImageField(upload_to=upload_thumb, null=True)
}}}

When I run makemigrations it produces the following (without error):

{{{
# encoding: utf8
from django.db import models, migrations
import apps.dinadesa.models


class Migration(migrations.Migration):

    dependencies = [
        ('dinadesa', '0006_auto_20140414_0836'),
    ]

    operations = [
        migrations.AlterField(
            model_name='photo',
            name='thumbnail',
            field=models.ImageField(null=True, upload_to=apps.dinadesa.models.upload_thumb),
        ),
    ]
}}}

(I keep my apps in an `apps` directory in the project root, in case that matters.)

When I try to run the migration, I get:

{{{
(dinadesa)$ django-admin migrate
Traceback (most recent call last):
  File ""/Users/dbinetti/.virtualenvs/dinadesa/bin/django-admin"", line 11, in <module>
    sys.exit(execute_from_command_line())
  File ""/Users/dbinetti/.virtualenvs/dinadesa/lib/python2.7/site-packages/django/core/management/__init__.py"", line 427, in execute_from_command_line
    utility.execute()
  File ""/Users/dbinetti/.virtualenvs/dinadesa/lib/python2.7/site-packages/django/core/management/__init__.py"", line 419, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File ""/Users/dbinetti/.virtualenvs/dinadesa/lib/python2.7/site-packages/django/core/management/base.py"", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File ""/Users/dbinetti/.virtualenvs/dinadesa/lib/python2.7/site-packages/django/core/management/base.py"", line 337, in execute
    output = self.handle(*args, **options)
  File ""/Users/dbinetti/.virtualenvs/dinadesa/lib/python2.7/site-packages/django/core/management/commands/migrate.py"", line 62, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File ""/Users/dbinetti/.virtualenvs/dinadesa/lib/python2.7/site-packages/django/db/migrations/executor.py"", line 14, in __init__
    self.loader = MigrationLoader(self.connection)
  File ""/Users/dbinetti/.virtualenvs/dinadesa/lib/python2.7/site-packages/django/db/migrations/loader.py"", line 48, in __init__
    self.build_graph()
  File ""/Users/dbinetti/.virtualenvs/dinadesa/lib/python2.7/site-packages/django/db/migrations/loader.py"", line 145, in build_graph
    self.load_disk()
  File ""/Users/dbinetti/.virtualenvs/dinadesa/lib/python2.7/site-packages/django/db/migrations/loader.py"", line 103, in load_disk
    migration_module = import_module(""%s.%s"" % (module_name, migration_name))
  File ""/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py"", line 37, in import_module
    __import__(name)
  File ""/Users/dbinetti/Repos/dinadesa/project/apps/dinadesa/migrations/0007_auto_20140414_0850.py"", line 6, in <module>
    class Migration(migrations.Migration):
  File ""/Users/dbinetti/Repos/dinadesa/project/apps/dinadesa/migrations/0007_auto_20140414_0850.py"", line 16, in Migration
    field=models.ImageField(null=True, upload_to=apps.dinadesa.models.upload_thumb),
AttributeError: 'module' object has no attribute 'upload_thumb'
}}}

Which I can work around by changing the `field` line in my migration to:

{{{
field=models.ImageField(null=True, upload_to=apps.dinadesa.models.Photo.upload_thumb),   # Note the addition of the `Photo` model in the path.
}}}

Then, the `migrate` command works fine.

BUT if I then try to run another migration it detects the altered field, and I have to do it all again.

I hope this is sufficient information.  It is my first bug report and I'm not certain how to create a test case, but it is 100% reproduce-able on my system.

Thanks.



"	Bug	closed	Migrations	1.7-beta-2	Release blocker	fixed		info@…	Accepted	1	0	1	0	0	0
