使用Django 1.7迁移的Python 2.7未绑定方法

时间:2015-07-03 22:56:24

标签: python django python-2.7 django-models django-south

我正在升级到Django 1.7.4并使用新的内置迁移,但在尝试运行echo stripslashes(str_replace('\r\n',PHP_EOL,$data));时出现以下错误:

$final = "";
while($row = $result->fetch_assoc()) {
    $final .= "[".$row["t_time"].", ".$row["t_value"]."],";
}
$final = substr($final, 0, -1);
echo $final;

我的模型定义:

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_callbackpath参数的方法。 Django的文档不清楚如何执行此操作,它仅指定attributeupload_to是必需的。

理想情况下,我想要的是:

instance

我有什么想法可以实现这个目标吗?

1 个答案:

答案 0 :(得分:7)

经过深思熟虑后,我偶然发现了这个Django错误报告。该解决方案似乎创建了一个可调用的工厂类:

https://code.djangoproject.com/ticket/22999

此处有更多讨论:Django - Cannot create migrations for ImageField with dynamic upload_to value