【发布时间】:2015-09-21 18:46:44
【问题描述】:
我正在升级到 Django 1.7.4 并使用新的内置迁移,但在尝试运行 makemigrations 时出现以下错误:
ValueError: Could not find function upload_callback in app_utils.
Please note that due to Python 2 limitations, you cannot serialize unbound
method functions (e.g. a method declared and used in the same class body).
Please move the function into the main module body to use migrations.For more information,
see https://docs.djangoproject.com/en/1.7/topics/migrations/#serializing-values
我的模型定义:
class Discount(models.Model):
banner = models.ImageField(
help_text=_("Banner image for this discount"),
upload_to=upload_to('discounts/', 'title'),
blank=True,
null=True
)
还有我在app_utils.py中的上传回调:
def upload_to(path, attribute=None):
def upload_callback(instance, filename):
compact = filename[:50]
try:
attr= unicode( slugify( getattr(instance, attribute) ) )
mypath = '%s/%s/%s' % (path, attr, compact)
except:
mypath = '%s%s' % (path, compact)
return mypath
return upload_callback
根据错误信息,表明upload_callback 未绑定。所以
我尝试将upload_callback 拉到包装函数之外,但找不到将额外的path 和attribute 参数传递给upload_to 的方法。 Django的文档不清楚如何做到这一点,它只指定instance和filename是必需的。
理想情况下,我想要的是:
def upload_to(instance, filename, path, attribute=None):
...
有什么想法可以实现这一目标吗?
【问题讨论】:
标签: python django python-2.7 django-models django-south