Django迁移错误名称' bPath'没有定义

时间:2015-02-27 16:32:50

标签: django django-models django-migrations

我正在使用Django 1.7.4。

# base.py
PRIVATE_FOLDER_ROOT = str(PROJECT_DIR.child('web_private'))
# tested: PRIVATE_FOLDER_ROOT = PROJECT_DIR.child('web_private')
# tested: PRIVATE_FOLDER_ROOT = '/var/www/project/project/web_private'

# models.py
from django.conf import settings

@python_2_unicode_compatible
class MessageFile(models.Model):
    """
    """
    fs_private_folder = FileSystemStorage(location=settings.PRIVATE_FOLDER_ROOT)

    message = models.ForeignKey(Message)
    file = models.FileField('file', storage=fs_private_folder, upload_to=get_upload_path_message_file)

当我跑

./manage.py migrate <my_app> --settings=myapp.settings.local

我收到以下错误

NameError:未定义名称“bPath”

检查0001_initial.py我发现未导入或定义bPath。

    fields=[
        ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
        ('file', models.FileField(upload_to=apps.admin_messages.models.get_upload_path_message_file, storage=django.core.files.storage.FileSystemStorage(location=bPath('/var/www/project/project/web_private')), verbose_name=b'file')),
        ('message', models.ForeignKey(to='admin_messages.Message')),
    ],

此SO question与我的相关联,但建议的解决方案不起作用。

谢谢,

d

1 个答案:

答案 0 :(得分:1)

看起来makemigrations会自动在字符串文字前面添加一个“b”,以便在Python 3中将它们标记为字节字符串。

如果将转换从str更改为unicode,可能会有所帮助,即:

PRIVATE_FOLDER_ROOT = unicode(PROJECT_DIR.child('web_private'))