我正在将我的Django App从Django 1.5.5 tot 1.9升级,Django-cms从2.4.3升级到3.3(以及所有相应的软件包)。
在我完成所有折旧函数的错误之后,我现在偶然发现了一个我无法理解的错误:'没有名为migration的模块'
我在运行时遇到此错误(在virtualenv中): - python manage.py runserver 而且当我跑 - python manage.py migrate
Traceback (most recent call last):
File "manage.py", line 20, in <module>
execute_from_command_line(sys.argv)
File "/var/www/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
utility.execute()
File "/var/www/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 342, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/var/www/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 195, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/var/www/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 39, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/var/www/env/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 16, in <module>
from django.db.migrations.autodetector import MigrationAutodetector
File "/var/www/env/local/lib/python2.7/site-packages/django/db/migrations/__init__.py", line 1, in <module>
from .migration import Migration, swappable_dependency # NOQA
ImportError: No module named migration
manage.py
#!/usr/bin/env python2
import os
import sys
if __name__ == "__main__":
settings_module_path = 'ais.settings.production'
########## Attempt to override settings using local settings
try:
from ais.settings.local_settings import *
# For developmentent, file will probably hold the following:
settings_module_path = 'ais.settings.development'
print "!!!manage.py settings overwritten!!!"
except ImportError:
pass
os.environ['DJANGO_SETTINGS_MODULE'] = settings_module_path
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
migrate.sh
#!/bin/sh
echo "Starting ..."
echo ">> Deleting old migrations"
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc" -delete
# Optional
echo ">> Deleting sqlite (if exists) database"
find . -name "db.sqlite3" -delete
echo ">> Running manage.py makemigrations"
python manage.py makemigrations
echo ">> Running manage.py migrate"
python manage.py migrate
echo ">> Done"
答案 0 :(得分:14)
您的脚本似乎是问题所在。它试图删除您的迁移,但它实际上也删除了Django的/django/db/migrations/
文件的内容。请注意,它显式不会删除__init__.py
文件,但会删除其他文件。
一种选择就是删除这些行:
echo ">> Deleting old migrations"
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc" -delete
一旦你在生产中运行Django,你不应该删除旧的迁移,因为你可能想要为迁移添加自定义代码。这看起来像是开发的便利脚本。
答案 1 :(得分:9)
如果您的错误仍然类似:
from .migration import Migration, swappable_dependency # NOQA
ImportError: No module named 'django.db.migrations.migration'
您需要重新安装dajngo
检查您的Django版本,然后强制重新安装
python -m django --version
pip install --upgrade --force-reinstall软件包
pip install --upgrade --force-reinstall Django==2.0.5
答案 2 :(得分:3)
只需重新安装Django版本或升级版本即可。这解决了我的问题。
pip install --upgrade django==1.11.18
然后进行移民
答案 3 :(得分:2)
正如@YPCrumble指出的那样,您的“ >>正在删除旧的迁移”脚本也删除了/django/db/migrations/
文件。要恢复它,您需要卸载Django并重新安装。
答案 4 :(得分:1)
您必须已删除迁移文件
在这种情况下,请尝试强制重新安装Django:
pip install --upgrade --force-reinstall Django
答案 5 :(得分:0)
Class User(models.Model):
username = models.CharField(max_length=255)
Class Meta:
manage = False
manage = True
答案 6 :(得分:0)
尝试将您的virtualenv存储库放置在项目之外。或者在不包含您的virtualenv存储库的路径中运行脚本。
或者您可以从查找路径中排除virtualenv存储库。选中How to exclude a directory in find . command