python - Django - Cannot create migrations for ImageField with dynamic upload_to value -
i upgraded app 1.7 (actually still trying).
this had in models.py:
def path_and_rename(path): def wrapper(instance, filename): ext = filename.split('.')[-1] # set filename random string filename = '{}.{}'.format(uuid4().hex, ext) # return whole path file return os.path.join(path, filename) return wrapper class userprofile(abstractuser): #... avatar = models.imagefield(upload_to=path_and_rename("avatars/"), null=true, blank=true, default="avatars/none/default.png", height_field="image_height", width_field="image_width")
when try makemigrations
, throws:
valueerror: not find function wrapper in webapp.models. please note due python 2 limitations, cannot serialize unbound method functions (e.g. method declared , used in same class body). please move function main module body use migrations.
i not sure if ok answer own question, figured out (i think).
according this bug report, edited code:
from django.utils.deconstruct import deconstructible @deconstructible class pathandrename(object): def __init__(self, sub_path): self.path = sub_path def __call__(self, instance, filename): ext = filename.split('.')[-1] # set filename random string filename = '{}.{}'.format(uuid4().hex, ext) # return whole path file return os.path.join(self.path, filename) path_and_rename = pathandrename("/avatars")
and then, in field definition:
avatar = models.imagefield(upload_to=path_and_rename, null=true, blank=true, default="avatars/none/default.png", height_field="image_height", width_field="image_width")
this worked me.
Comments
Post a Comment