We dynamically generate the upload_to destination in our project using a callable, like below.
class PKUploader(object):
def __init__(self, prefix, extension=None):
self.prefix = prefix
self.extension = extension
def __call__(self, instance, filename):
ext = self.extension or os.path.splitext(filename)[1]
if not ext.startswith('.'):
ext = '.' + ext
if instance.pk is None:
pk = random.randint(1000, 99999)
else:
pk = instance.pk
pk = str(pk) + str(uuid.uuid4())
return '{}/{}{}'.format(self.prefix, pk, ext)
And in our models it's used like
class CoolKids(models.Model):
image = models.ImageField(upload_to=PKUploader('users_image'))
However, when we run upgraded from django 1.6 to 1.7 and run makemigrations we get the following error.
ValueError: Cannot serialize: <PKUploader object at 0x7ff5f1cf0b90>
There are some values Django cannot serialize into migration files.
For more, see https://docs.djangoproject.com/en/dev/topics/migrations/#migration-serializing
Can any one shed some light into this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…