【问题标题】:Django migrate error name 'bPath' is not defined未定义 Django 迁移错误名称“bPath”
【发布时间】:2015-02-27 16:32:50
【问题描述】:

我在 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: name '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

【问题讨论】:

  • @Selcuk 谢谢这是解决方案。请您删除评论并添加答案。我在这个问题上花了几个小时,我没有考虑 unicode cast 而不是 string....

标签: django django-models django-migrations


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2013-02-09
    • 2014-05-19
    • 2017-12-13
    • 1970-01-01
    • 1970-01-01
    • 2013-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多