Home > other >  Django app NameError however the app is installed
Django app NameError however the app is installed

Time:07-04

I'm trying to modify Django's built-in authetnication system by adding a custom user model. The customized model is defined inside an app named accounts:

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

# Create your models here.

COUNTRIES = settings.COUNTRY_CHOICES

class CustomUser(AbstractUser):
    zip_code = models.PositiveSmallIntegerField(blank=True, null=True)
    city = models.CharField(blank=True, null=True, max_length=64)
    address_street = models.CharField(blank=True, null=True, max_length=64)
    address_number = models.CharField(blank=True, null=True, max_length=32)
    country = models.CharField(blank=True, null=True, choices=COUNTRIES, max_length=8)

I updated the settings.py file this way:

AUTH_USER_MODEL = accounts.CustomUser

I added accounts as an installed app:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'accounts',
    'store',
    'cart',
]

When I try to run the python manage.py makemigrations command in terminal to apply changes made to the user model Django's throwing this error:

Traceback (most recent call last):
  File "C:\Users\uhlar\Dev\ecommerce\manage.py", line 22, in <module>
    main()
  File "C:\Users\uhlar\Dev\ecommerce\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\uhlar\.virtualenvs\ecommerce-vU6zMECh\lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line        
    utility.execute()
  File "C:\Users\uhlar\.virtualenvs\ecommerce-vU6zMECh\lib\site-packages\django\core\management\__init__.py", line 386, in execute
    settings.INSTALLED_APPS
  File "C:\Users\uhlar\.virtualenvs\ecommerce-vU6zMECh\lib\site-packages\django\conf\__init__.py", line 87, in __getattr__
    self._setup(name)
  File "C:\Users\uhlar\.virtualenvs\ecommerce-vU6zMECh\lib\site-packages\django\conf\__init__.py", line 74, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Users\uhlar\.virtualenvs\ecommerce-vU6zMECh\lib\site-packages\django\conf\__init__.py", line 183, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "C:\Users\uhlar\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "C:\Users\uhlar\Dev\ecommerce\config\settings.py", line 149, in <module>
    AUTH_USER_MODEL = accounts.CustomUser
NameError: name 'accounts' is not defined

The error is not only confined to the accounts app, no matter inside which app I try to define the CustomUser model I'm still getting it.

If anyone knows what's causing the problem please help me to resolve it.

CodePudding user response:

The AUTH_USER_MODEL setting [Django-doc] should be a string, not a (qualified) name, so:

# settings.py

# …

#  string literal            
  • Related