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

24πŸ‘

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.

πŸ‘€Elias Prado

8πŸ‘

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

πŸ‘€yaderv

2πŸ‘

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.

1πŸ‘

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
    
πŸ‘€Ornstein89

0πŸ‘

upgrade Django version

pip install django –upgrade

-4πŸ‘

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__?

Leave a comment