Я обновление до Django 1.7.4 и использовать новые встроенные миграции, но я получаю следующее сообщение об ошибке при попытке запуска 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
за пределы функции обертки, но не смог найти способ передать дополнительные параметры path
и attribute
, переданные в upload_to
. Документация Django неясно, как это сделать, и это только указывает, что требуются instance
и filename
.
В идеале, я хотел бы это:
def upload_to(instance, filename, path, attribute=None):
...
Любые идеи, как я могу идти о достижении этой цели?