0

First of all: I am well aware that there are several questions with this exact error but I've read at least 20 of them and none helped me correct this error.

This being said, the problem is the following:

I created a directory called 'api' inside my Django project to store all the code related to the api. Inside this directory I created an app called 'profiles_app'. There, in the models.py file I defined a UserProfile class to override the default Django UserProfile class. I then added the app to the INSTALLED_APPS variable in the settings.py file and overrid the default UserProfile class with the following line:

AUTH_USER_MODEL = 'profiles_api.UserProfile'

Finally I tried to make the migrations to see the new table in the database and this is when the error occurred.

This is the stack trace:

    (venv) C:\Users\Juan Pablo\Documents\backend\backend_django>python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\JUANPA~1\Envs\venv\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\Users\JUANPA~1\Envs\venv\lib\site-packages\django\core\management\__init__.py", line 377, in execute
    django.setup()
  File "C:\Users\JUANPA~1\Envs\venv\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\JUANPA~1\Envs\venv\lib\site-packages\django\apps\registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "C:\Users\JUANPA~1\Envs\venv\lib\site-packages\django\apps\config.py", line 90, in create
    module = import_module(entry)
  File "C:\Users\JUANPA~1\Envs\venv\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 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'api'

As you can see in the trace I am working in a virtual environment and this is what I have installed:

asgiref==3.2.3
Django==3.0.3
djangorestframework==3.11.0
mysqlclient==1.4.6
pytz==2019.3
sqlparse==0.3.0

This is the project's structure:

The init.py file inside the api directory wasn't there at first but after reading some answers I created a Python package instead of a directory and then that file appeared.

Finally, this is what the INSTALLED_APPS variable in the settings.py file looks like:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'rest_framework.authtoken',
    'api.profiles_api',
]

I have tried the following:

  • 'profiles_api' instead of 'api.profiles_api' in INSTALLED_APPS

  • AUTH_USER_MODEL = 'api.profiles_api.UserProfile' instead of 'profiles_api.UserProfile'

  • Creating a Python package named 'api' instead of a directory.

Any help is really appreciated. Sorry for the long question but I wanted to be as precise as possible.

Zufra
  • 574
  • 1
  • 7
  • 16
  • Does this answer your question? [Django settings.AUTH\_USER\_MODEL defined in separate module](https://stackoverflow.com/questions/50179252/django-settings-auth-user-model-defined-in-separate-module) – Scircia Feb 16 '20 at 22:57
  • Not really. That question had a problem with the naming of his app and thus had to configure the apps.py file. I don't have to rename anything so I can use the default apps.py file. – Zufra Feb 16 '20 at 23:14
  • Have you added `api` as an installed app? (Your structure is a little unusual) – markwalker_ Feb 16 '20 at 23:16
  • @markwalker_ api is just a folder, not an app. I don't want every app to be in the root directory. What do you mean by the structure being unusual? Anyway, I tried adding it as an installed app but it didn't work. – Zufra Feb 16 '20 at 23:29
  • @Zufra You are tried add to `INSTALLED_APPS` `profiles_api` only (without `api`)? – unknown Feb 17 '20 at 01:03
  • Yes, I stated that in the question. – Zufra Feb 17 '20 at 14:15

1 Answers1

0

I made a new project and structured as follows:

enter image description here

Then in installed apps I added 'backend_project.Users' and it worked. Still don't know why the original question was wrong though.

Zufra
  • 574
  • 1
  • 7
  • 16