For a Django rest_framework project I wanted to create several models (User and Contributor model in 'accounts' and Project, Issue and Comment in 'project'). Normally I create several apps to create these models.
for visualisation, project tree with several apps:
# project tree
.
├── accounts
│ ├── __init__.py
│ ├── models.py
├── api
├── main.py
├── manage.py
├── project
│ ├── __init__.py
│ ├── models.py
└── project_name
├── asgi.py
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
I realized that it is really hard to implement the Router for the Django rest_framework, I was not able to make it work. So, I had to come up with an other strategy.
now my project tree:
# current project tree
.
├── api
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ ├── models
│ │ ├── accounts.py
│ │ ├── __init__.py
│ │ ├── project.py
│ ├── serializers
│ │ ├── accounts.py
│ │ ├── __init__.py
│ │ └── project.py
│ ├── urls
│ │ ├── accounts.py
│ │ ├── __init__.py
│ │ └── project.py
│ └── views
│ ├── accounts.py
│ ├── __init__.py
│ └── project.py
├── db.sqlite3
├── manage.py
└── project_name
├── asgi.py
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
I add the the app and the rest_framework variable into the settings.py
file:
# settings.py
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
# custom api
"api",
"rest_framework",
]
I create the models for custom User and Contributor in models directory in accounts.py
file. And the models for Project, Issue and Comment is located inside the models directory in project.py
.
In settings.py
I set:
# settings.py
AUTH_USER_MODEL = "api.User"
The AUTH_USER_MODEL has to be defined as: "app_label.ModelName"
My app name is "api" and the modelname is "User"
Add the models into the admin.py
file.
After creating the models I wanted to make the migrations.
After using python manage.py migrations
I got the error:
# terminal
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'api.User' that has not been installed
This error typically occurs when there is an issue with the configuration of the custom user model in Django.
To get rid of this error, in my case, I had to add this inside the __init__.py
file of the models directory.
# models/__init__.py
from .accounts import * # noqa
To ensure that the User model is found I had to import all contents of the accounts.py
file.
noqa
most likely stands for "no quality assurance". It tells code-analysis software to ignore warnings.
After adding this line into the __init__.py
file the python manager.py makemigrations
was working fine.
If is possible to just import parts of the accounts.py
file.
Top comments (0)