Django NameError:name' bPath'没有定义

时间:2014-12-04 20:37:22

标签: 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

这是我的行为/ 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)

我的媒体声明:

BASE_DIR = Path(__file__).ancestor(3)
MEDIA_ROOT = BASE_DIR.child("media")

2 个答案:

答案 0 :(得分:7)

如果没有jantiyes.settings.base.MEDIA_ROOT的确切定义,很难判断,但我想它是一个类bPath)的实例,而不是deconstructible和那是subclass of 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可解构

答案 1 :(得分:1)

MEDIA_ROOT在哪里定义?我假设它已在您的设置文件中定义,在这种情况下您可能需要

from django.conf import settings

upload_storage = FileSystemStorage(location=settings.MEDIA_ROOT)