我正在升级到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):
...
有什么想法能帮我实现这个吗?