1

I am working on a Django project and getting the following error when trying to import the User model from chat_app_backend.authentication in my models.py file:

ModuleNotFoundError: No module named 'chat_app_backend.authentication'

Here's the code from my models.py file from my chat.models:

from django.db import models
from chat_app_backend.authentication.models import User

class ChatRoom(models.Model):
    owner = models.ForeignKey(User, on_delete=models.CASCADE)
    picture = models.ImageField(upload_to='chatroom_pictures/', null=True, blank=True)
    number_of_users = models.IntegerField()
    messages = models.TextField()

Here's my installed apps:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'authentication.apps.AuthenticationConfig',
    'chat.apps.ChatConfig'
]

here's my models.py of my authentication.models:

from django.contrib.auth.models import AbstractUser
from django.db import models

class User(AbstractUser):
    bio = models.CharField(max_length=500)
    profile_picture = models.ImageField(upload_to='profile_pictures/')

And here's my project's file structure:

chat_app_backend
    -chat_app_backend
        -authentication
        -chat
        -chat_app_backend

I have tried the solution provided in this post (Django : Unable to import model from another App), but it did not work for me.

Apparently, its not the issue related to only the user model, when I try to access even the UserSerializer, which is in the authentication app(serializers.py), its giving me the same error.

from chat_app_backend.authentication.serializers import UserSerializer
ModuleNotFoundError: No module named 'chat_app_backend.authentication'

here's the code for the serializers.py in the chat app:

from rest_framework import serializers
from .models import ChatRoom
from chat_app_backend.authentication.serializers import UserSerializer


class ChatRoomSerializer(serializers.ModelSerializer):
    owner = UserSerializer(read_only=True)

    class Meta:
        model = ChatRoom
        fields = ['id', 'owner', 'picture', 'number_of_users', 'messages']

2 Answers2

1

You import this with:

from authentication.models import User

But it is usually better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model. Indeed, in the settings you specify this as:

# settings.py

AUTH_USER_MODEL = 'authentication.User'

and then use the settings instead:

from django.conf import settings
from django.db import models


class ChatRoom(models.Model):
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    # …

Regardless what option you decide, likely you have set the "source root" in your IDE on the wrong directory. This should be the chat_app_backend in the chat_app_backend. It might also be better to give the directories a different name, especially if a directory or file has the same name as its parent directory, this can introduce a lot of confusion.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • is there some way to use the User model without using the settings.AUTH_USER_MODEL? – sneaky dude Apr 16 '23 at 14:45
  • 1
    @sneakydude: that would be horrible code design. But as said, there are two ways to solve it. But the former is only fixing the specific problem. The `AUTH_USER_MODEL` is a setting used by a lot of Django packages to link things to... the user model you use to represent the user. – Willem Van Onsem Apr 16 '23 at 14:48
  • ohh can we use the fields related to the custom user model as well? – sneaky dude Apr 16 '23 at 14:49
  • also should i use settings.AUTH_USER_MODEL everywhere i used from authentication.models import User? – sneaky dude Apr 16 '23 at 14:51
  • @sneakydude: yes, normally you use the setting to refer to the user model: https://docs.djangoproject.com/en/dev/topics/auth/customizing/#referencing-the-user-model if you then later change your user model, you can effectively swap in a different user model. – Willem Van Onsem Apr 16 '23 at 14:52
  • I have edited my question, its giving me an error even when I try to import a serializer from my authentication app's serializers.py to my chat app's serializers.py – sneaky dude Apr 16 '23 at 15:04
  • @sneakydude: you should import it with `from authentication.serializers import UserSerializer`. The root directory should be the ane immediately above the `authentication`, so it is always `from authentication...`. – Willem Van Onsem Apr 16 '23 at 15:39
0

try this:

import os
import sys

# Add the chat_app_backend directory to the Python path
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from chat_app_backend.authentication.models import User

os.path.abspath(__file__) gives you the absolute path of the current file which is models.py. Then os.path.dirname() function returns the directory containing the file which here is chat directory. Finally we use sys.path.append() to add it to the Python path.

Phoenix
  • 1,343
  • 8
  • 10