Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django Custom user model, the model saved in a separated file inside folder

I am new to Django and working on a project that needs a custom user. I created folders for models, views, URLs, and serializers. Each feature has its own file in these folders.
Everything works fine until I rename models.py file to users_model.py or move it to the models folder that I created.
When I makemigrations the following error appears:

django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'app.AppUser' that has not been installed

backend/
├── config/
│   ├── __pycache__/
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   ├── wsgi.py
├── app/
│   ├── __pycache__/
│   ├── migrations/
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── models
│   │   ├── users_model.py
│   │   ├── model_for_featur_1
│   │   ├── model_for_featur_2
│   ├── serializer
│   │   ├── users_serializer.py
│   │   ├── serialize_for_featur_1
│   │   ├── serialize_for_featur_2
│   ├── views
│   │   ├── users_view.py
│   │   ├── view_for_featur_1
│   │   ├── view_for_featur_2
│   ├── urls
│   │   ├── url_for_featur_1
│   │   ├── url_for_featur_2
├── manage.py

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My code:

users_model.py

class AppUser(AbstractBaseUser):
  .
  .
  .

class MyUserManager(BaseUserManager):
    def create_user(self, email, username, password):
        .
        .
    def create_superuser(self, email, username, password):
        .
        .

Admin.py

   .
   .
admin.site.register(AppUser)

sittings.py

INSTALLED_APPS = [
     .
     .
   "app",
 ]

AUTH_USER_MODEL = "app.AppUser"

I tried AUTH_USER_MODEL = "app.models.users_model.AppUser"

ValueError: Invalid model reference 'app.models.users_model.AppUser'. String model references must be of the form 'app_label.ModelName'.

I tried AUTH_USER_MODEL = "app.models.AppUser"

ValueError: Invalid model reference 'app.models.AppUser'. String model references must be of the form 'app_label.ModelName'.

>Solution :

you need to create __init__.py file in models folder and it looks like that:

## __init__.py
from .user import AppUser
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading