RuntimeError: __class__ not set defining 'AbstractBaseUser' as <class 'django.contrib.auth.base_user.Abstract BaseUser'>. Was __classcell__ propagated

Viewed 12455
  1. I'm getting this error while trying to migrate databases after manually setting up PostgreSQL database for the project.
  2. I was trying to run the cloned quora project forked from github when this error cropped up.

Follow the full error description:

RuntimeError: __class__ not set defining 'AbstractBaseUser' as <class 'django.contrib.auth.base_user.Abstract BaseUser'>. Was __classcell__ propagated to type.__new__?

Follow the trace:

    Traceback (most recent call last):
      File "manage.py", line 10, in <module>
        execute_from_command_line(sys.argv)
          File "C:\Users\user\Desktop\quora-clone-master\env\lib\site-packages\django\core\management\__init__.py", line 353, in execute_from_command_line
            utility.execute()
          File "C:\Users\user\Desktop\quora-clone-master\env\lib\site-packages\django\core\management\__init__.py", line 327, in execute
            django.setup()
          File "C:\Users\user\Desktop\quora-clone-master\env\lib\site-packages\django\__init__.py", line 18, in setup
            apps.populate(settings.INSTALLED_APPS)
          File "C:\Users\user\Desktop\quora-clone-master\env\lib\site-packages\django\apps\registry.py", line 108, in populate
            app_config.import_models(all_models)
          File "C:\Users\user\Desktop\quora-clone-master\env\lib\site-packages\django\apps\config.py", line 202, in import_models
            self.models_module = import_module(models_module_name)
          File "C:\Users\user\Desktop\quora-clone-master\env\lib\importlib\__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:\Users\user\Desktop\quora-clone-master\env\lib\site-packages\django\contrib\auth\models.py", line 4, in <module>
            from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
          File "C:\Users\user\Desktop\quora-clone-master\env\lib\site-packages\django\contrib\auth\base_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__?
6 Answers

This approach solved my problem using django - version 1.9.5. Therefore, if you need to run an old Django application with new python check the following approach:

If your are using virtual environment go to: venv/lib/Python3.8/site-packages/django/db/models/base.py. Then, on the class ModelBase(type): model add the following code:

(MAKE SURE YOU ADD ONLY THE CODE SPECIFIED)

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 .....

Now, go to the directory that the file manage.py is located and migrate your application.

Then, python manage.py run.

I've got this answer from this website.

Check your python and django versions. I had the same problem and had to install a previous python with via pyenv.

Occurence

While installing some python package the pipenv (package manager) downgraded django version to some 1.0.x while my python version in virtual env was 3.8.5.

Solution

Reinstalled django to latest version 3.2.4 to match latest version of python 3.8.5.

I solved similar RuntimeError: __class__ not set defining 'AbstractBaseUser' as ... error (it was some legacy django 1.9 project) by using virtualenv and binding it to Python 3.6 installation:

  1. Install virtualenv:

    pip3 install virtualenv
    
  2. Download old Python 3.6 and install it with minimal options (only pip checkbox and no "Add to PATH" in options) to some path (I used that django project directory itself, /my_django_project/python36 for example).

  3. Create virtual environment, binding it to old python:

    cd /my_django_project
    virtualenv --python=./python36/python.exe venv
    
  4. Activate virtualenv:

    .\venv\Scripts\activate
    
  5. Install old django==1.9 (and other packages when necessary):

    (venv) pip install django==1.9
    
  6. Launch django backend in activated virtual environment:

    (venv) python manage.py runserver
    

upgrade Django version

pip install django --upgrade

Consider this line

class AbstractBaseUser(models.Model):
RuntimeError: __class__ not set defining 'AbstractBaseUser'as<class'django.contrib.auth.base_user.AbstractBaseUser'>. Was __classcell__ propagated to type.__new__?
Related