PyCharm 中的 Django 迁移
Posted
技术标签:
【中文标题】PyCharm 中的 Django 迁移【英文标题】:Django makemigrations in PyCharm 【发布时间】:2015-11-04 06:38:28 【问题描述】:当我向我的模型添加没有默认值的新字段时
some_field = models.IntegerField()
然后运行 makemigrations 我收到这条消息
`You are trying to add a non-nullable field 'some_field' to sometable without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows)
2) Quit, and let me add a default in models.py
Select an option: `
当我在普通控制台中选择第一个选项时,它会设置默认值并且工作正常,但是当我在 pycharm manage.py 控制台中执行相同操作时,我会收到此错误
Traceback (most recent call last):
File "/opt/pycharm-4.5.2/helpers/pycharm/django_manage.py", line 41, in <module>
run_module(manage_file, None, '__main__', True)
File "/usr/lib/python2.7/runpy.py", line 176, in run_module
fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 82, in _run_module_code
mod_name, mod_fname, mod_loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/home/dima/Python/nflstats/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/dima/envs/nflstats_env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/home/dima/envs/nflstats_env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/dima/envs/nflstats_env/local/lib/python2.7/site-packages/django/core/management/base.py", line 393, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/dima/envs/nflstats_env/local/lib/python2.7/site-packages/django/core/management/base.py", line 444, in execute
output = self.handle(*args, **options)
File "/home/dima/envs/nflstats_env/local/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 125, in handle
migration_name=self.migration_name,
File "/home/dima/envs/nflstats_env/local/lib/python2.7/site-packages/django/db/migrations/autodetector.py", line 43, in changes
changes = self._detect_changes(convert_apps, graph)
File "/home/dima/envs/nflstats_env/local/lib/python2.7/site-packages/django/db/migrations/autodetector.py", line 185, in _detect_changes
self.generate_added_fields()
File "/home/dima/envs/nflstats_env/local/lib/python2.7/site-packages/django/db/migrations/autodetector.py", line 763, in generate_added_fields
self._generate_added_field(app_label, model_name, field_name)
File "/home/dima/envs/nflstats_env/local/lib/python2.7/site-packages/django/db/migrations/autodetector.py", line 793, in _generate_added_field
field.default = self.questioner.ask_not_null_addition(field_name, model_name)
File "/home/dima/envs/nflstats_env/local/lib/python2.7/site-packages/django/db/migrations/questioner.py", line 137, in ask_not_null_addition
return self._ask_default()
File "/home/dima/envs/nflstats_env/local/lib/python2.7/site-packages/django/db/migrations/questioner.py", line 111, in _ask_default
code = input(">>> ").decode(sys.stdin.encoding)
TypeError: decode() argument 1 must be string, not None
我不明白,我做错了什么?为什么它在pycharm中不起作用?我怎样才能解决这个问题? 我使用 PyCharm 4.5.2 专业版
【问题讨论】:
我的意思是这没什么大不了的,只需将整数字段设置为 default=0,它与 shell 中的选项 1 的概念相同。 【参考方案1】:我可以确认这是一个错误。从 4.0.0 开始,我在 Windows 10 和 Mac OS X Mavericks 上都遇到了这个问题。
我宁愿在加载我的 Virtualenv 后保持终端打开并从那里运行命令。
【讨论】:
以上是关于PyCharm 中的 Django 迁移的主要内容,如果未能解决你的问题,请参考以下文章
搭建 python 3.5+pycharm 2017.1.3+django 1.12.0 首次 将sqlite3 迁移到mysql
Django项目的创建与介绍.应用的创建与介绍.启动项目.pycharm创建启动项目.生命周期.三件套.静态文件.请求及数据.配置Mysql完成数据迁移.单表ORM记录的增删改查