Django NameError:未定义名称"bPath"

Yan*_*ick 6 django django-models

我正在运行Django 1.7,当我运行python manage.py migrate时,我收到以下错误

      File "/home/ymorin007/workspace/sites/jantiyes.com/src/deeds/migrations/0006_auto_20141204_1631.py", line 9, in <module>
    class Migration(migrations.Migration):
  File "/home/ymorin007/workspace/sites/jantiyes.com/src/deeds/migrations/0006_auto_20141204_1631.py", line 19, in Migration
    field=models.ImageField(storage=django.core.files.storage.FileSystemStorage(location=bPath('/home/ymorin007/workspace/sites/jantiyes.com/src/media')), max_length=255, null=True, upload_to=deeds.models.picture_name, blank=True),
NameError: name 'bPath' is not defined
Run Code Online (Sandbox Code Playgroud)

这是我的行为/ models.py

from jantiyes.settings.base import MEDIA_ROOT

upload_storage = FileSystemStorage(location=MEDIA_ROOT)

def picture_name(self, filename):

    ext = filename.split('.')[-1]
    deedname = re.sub('[ ]', '-', self.text.lower())
    filename = "DEED-%s-%s.%s" % (self.id, deedname, ext)

    url = "%s" % filename

    return url


class Deed(TimeStampedModel):

    picture = models.ImageField(upload_to=picture_name, null=True, blank=True, storage=upload_storage, max_length=255)
    text = models.CharField(max_length=500)
    when = models.DateField(unique=True)
Run Code Online (Sandbox Code Playgroud)

我的媒体宣言:

BASE_DIR = Path(__file__).ancestor(3)
MEDIA_ROOT = BASE_DIR.child("media")
Run Code Online (Sandbox Code Playgroud)

Sim*_*tte 7

很难说没有确切的定义jantiyes.settings.base.MEDIA_ROOT,但我想这是一个类(实例bPath)不是deconstructible,这是一个子类unicode.因此,迁移编写者认为它不需要任何导入,只需要repr原来的值bPath('/home/ymorin007/workspace/sites/jantiyes.com/src/media').

您有两种选择:

  1. 确保jantiyes.settings.base.MEDIA_ROOT被定义为字符串,因此由迁移编写者正确处理.例如MEDIA_ROOT = '/home/ymorin007/workspace/sites/jantiyes.com/src/media'在你的jantiyes.settings.base模块文件中.
  2. 通过定义将导入路径返回给自身的方法,确保bPath该类是可解构deconstruct.