2015-07-03 125 views
4

我升级到1.7.4的Django和使用新的内置的迁移,但我得到尝试运行makemigrations如果出现以下错误:的Python 2.7和Django 1.7迁移不受约束的方法

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): 
    ... 

任何想法如何,我可以去实现这一目标?

回答