Python 2.7与Django 1.7迁移中未绑定的方法问题

4

我正在升级到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从包装函数中提取出来,但找不到一种方法来传递额外的pathattribute参数到upload_to中。Django的文档没有明确说明如何做到这一点,它只指定了需要instancefilename

理想情况下,我希望是:

def upload_to(instance, filename, path, attribute=None):
    ...

有什么想法能帮我实现这个吗?
1个回答

7

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接