dnit13 dnit13 - 10 days ago 7
Python Question

Changing callable for upload_to breaks migrations Django

I had a callable for my image uploads which looked like this:

@deconstructible
class Rename(object):
def __init__(self, path):
self.path = path

def __call__(self, instance, filename):
# some stuff and returns path


when I changed the
__init__
signature to :

def __init__(self, path, file_type):
self.path = path
self.file_type = file_type


I can no longer run makemigrations:

File "/pathtomyapp/migrations/0001_initial.py", line 59, in Migration
('avatar', models.ImageField(blank=True, null=True, upload_to=myapp.models.Rename(b'profiles'))),
TypeError: __init__() takes exactly 3 arguments (2 given)


Is there any way to solve this without modifying Rename functions signature in migration files

Answer

You could either change the migration file and provide the file_type

('avatar', models.ImageField(blank=True, null=True, upload_to=myapp.models.Rename(b'profiles', 'file-type'))),

Or make the file_type argument optional in your __init__ method.

@deconstructible
class Rename(object):
    def __init__(self, path, file_type=None):
Comments