[Answered ]-Upgrading from django 1.6 to 1.7 getting callable is not serialize when running makemigrations

2πŸ‘

βœ…

To serialize an instance of an arbitrary class, you need to implement a deconstruct() method:

class PKUploader(object):
    def __init__(self, prefix, extension=None):
        self.prefix = prefix
        self.extension = extension

    def deconstruct(self):
        kwargs = {'prefix': self.prefix}
        if self.extension is not None:
            kwargs['extension'] = self.extension
        return 'import.path.to.PKUploader', (), kwargs

    def __call__(self, instance, filename):
        ...

By returning the import path to the class, and the positional and keyword initialisation arguments, Django can serialize this information to valid python code that recreates the original instance.

To simplify this, you can use the @deconstructible decorator:

from django.utils.deconstruct import deconstructible

@deconstructible
class PKUploader(object):
    ...

See the documentation for full details.

πŸ‘€knbk

0πŸ‘

You should avoid this error if you assign the callable to a variable.

upload_to = PKUploader('users_image')

class CoolKids(models.Model):
    image = models.ImageField(upload_to=upload_to)
πŸ‘€Alasdair

Leave a comment