- 在为项目手动设置PostgreSQL数据库后,我在尝试迁移数据库时遇到此错误
- 出现此错误时,我正试图运行从github派生的克隆quora项目
遵循完整的错误描述:
RuntimeError: __class__ not set defining 'AbstractBaseUser' as <class 'django.contrib.auth.base_user.Abstract BaseUser'>. Was __classcell__ propagated to type.__new__?
跟踪:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:UsersuserDesktopquora-clone-masterenvlibsite-packagesdjangocoremanagement__init__.py", line 353, in execute_from_command_line
utility.execute()
File "C:UsersuserDesktopquora-clone-masterenvlibsite-packagesdjangocoremanagement__init__.py", line 327, in execute
django.setup()
File "C:UsersuserDesktopquora-clone-masterenvlibsite-packagesdjango__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:UsersuserDesktopquora-clone-masterenvlibsite-packagesdjangoappsregistry.py", line 108, in populate
app_config.import_models(all_models)
File "C:UsersuserDesktopquora-clone-masterenvlibsite-packagesdjangoappsconfig.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "C:UsersuserDesktopquora-clone-masterenvlibimportlib__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:UsersuserDesktopquora-clone-masterenvlibsite-packagesdjangocontribauthmodels.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "C:UsersuserDesktopquora-clone-masterenvlibsite-packagesdjangocontribauthbase_user.py", line 49, in <module>
class AbstractBaseUser(models.Model):
RuntimeError: __class__ not set defining 'AbstractBaseUser' as <class 'django.contrib.auth.base_user.AbstractBaseUser'>. Was __classcell__ propagated to type.__new__?
这种方法使用django - version 1.9.5
解决了我的问题。因此,如果您需要使用新的python运行旧的Django应用程序,请检查以下方法:
如果您正在使用虚拟环境,请转到:venv/lib/Python3.8/site-packages/django/db/models/base.py
。然后,在class ModelBase(type):
模型上添加以下代码:
(确保只添加指定的代码(
class ModelBase(type):
"""
Metaclass for all models.
"""
def __new__(cls, name, bases, attrs):
super_new = super(ModelBase, cls).__new__
# Also ensure initialization is only performed for subclasses of Model
# (excluding Model class itself).
parents = [b for b in bases if isinstance(b, ModelBase)]
if not parents:
return super_new(cls, name, bases, attrs)
# Create the class.
module = attrs.pop('__module__')
new_class = super_new(cls, name, bases, {'__module__': module})
# <========== THE CODE BELLOW SHOULD BE ADDED ONLY ======>>
new_attrs = {'__module__': module}
classcell = attrs.pop('__classcell__', None)
if classcell is not None:
new_attrs['__classcell__'] = classcell
new_class = super_new(cls, name, bases, new_attrs)
# <========== THE CODE ABOVE SHOULD BE ADDED ONLY ======>>
# the rest of the class .....
现在,转到文件manage.py
所在的目录并迁移您的应用程序。
然后,python manage.py run
。
我从这个网站上得到了这个答案。
检查您的python和django版本。我也遇到了同样的问题,不得不通过pyenv安装以前的python。
发生
在安装一些python包时,pipenv
(包管理器(将django
版本降级为一些1.0.x
,而我在虚拟环境中的python版本是3.8.5
。
解决方案
已将django
重新安装到最新版本3.2.4
,以匹配最新版本的python3.8.5
。
我通过使用virtualenv
并将其绑定到Python 3.6安装:解决了类似的RuntimeError: __class__ not set defining 'AbstractBaseUser' as ...
错误(它是一些遗留的django 1.9项目(
-
安装
virtualenv
:pip3 install virtualenv
-
下载旧的Python 3.6,并用最少的选项(只有pip复选框,选项中没有"添加到PATH"(将其安装到某个路径(例如,我使用了django项目目录本身,
/my_django_project/python36
(。 -
创建虚拟环境,将其绑定到旧python:
cd /my_django_project virtualenv --python=./python36/python.exe venv
-
激活虚拟:
.venvScriptsactivate
-
安装旧的
django==1.9
(以及必要时的其他软件包(:(venv) pip install django==1.9
-
在激活的虚拟环境中启动django后端:
(venv) python manage.py runserver
升级Django版本
pip安装django——升级
考虑这一行
class AbstractBaseUser(models.Model):
RuntimeError: __class__ not set defining 'AbstractBaseUser'as<class'django.contrib.auth.base_user.AbstractBaseUser'>. Was __classcell__ propagated to type.__new__?