pythondjangodjango-modelsbackenddjango-custom-user

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

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