python - Django makemigrations错误

标签 python django

大家好,我有一个关于数据库迁移的问题。我正在尝试从 2.7 切换到 3.4,这是我进行迁移时得到的结果

enter image description here

adgc@mnl-adgc-pc1:~/Desktop/people$ python3 manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 441, in execute
     output = self.handle(*args, **options)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/commands/makemigrations.py", line 125, in handle
    migration_name=self.migration_name,
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/autodetector.py", line 43, in changes
    changes = self._detect_changes(convert_apps, graph)
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/autodetector.py", line 186, in _detect_changes
    self.generate_altered_fields()
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/autodetector.py", line 850, in generate_altered_fields
    if old_field_dec != new_field_dec:
  File "/usr/local/lib/python3.4/dist-packages/django/core/validators.py", line 55, in __eq__
    self.regex.pattern == other.regex.pattern and
AttributeError: 'bytes' object has no attribute 'pattern'

最佳答案

python2 上的 Django 在代码中使用字节字符串创建迁移:

apps/post/migrations/0001_initial.py:                ('picture', models.ImageField(upload_to=b'images/posts/%Y_%m/%d', null=True, verbose_name='Picture', blank=True)),
apps/post/migrations/0001_initial.py:                ('lang_code', models.ForeignKey(default=b'en', verbose_name='Language', to='account.Language')),

当我们在 python3 环境中运行它时,它崩溃了。

$ find apps -type f -exec sed -i "s/{b'/{'/g" {} \;        
$ find apps -type f -exec sed -i "s/(b'/('/g" {} \;
$ find apps -type f -exec sed -i "s/ b'/ '/g" {} \;
$ find apps -type f -exec sed -i "s/=b'/='/g" {} \;
$ find apps -type f -exec sed -i "s/\[b'/\['/g" {} \;

See it over here too

关于python - Django makemigrations错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34152138/

相关文章:

python - python中如何声明子类中要实现的变量?

python - 如何在 Django 中编写自己的装饰器?

python - 如何在 Python 中将 Word 文档转换为非常简单的 html?

python - Django/HTML 提交按钮重定向到错误页面

python - 如何将标准输出转换为字符串 (Python)

Python 安装程序 : "Error writing to file C:\Python27\pythonw.exe"

python - 数据流中的值错误 : Invalid GCS location: None

python - 确定 Python 函数中返回值的数量

python - geodjango 检查 PolygonField 中的 PointField

django - 我如何在序列化器中计算?