Issue
I am developing a django project, the main directory/project name is django-basic-ecommerce
.
One of the apps name is accounts
(note that it has the final "s").
as I run
python manage.py makemigrations
I get
Traceback (most recent call last):
File "/my/path/basic-django-ecommerce/venv/lib/python3.10/site-packages/django/apps/registry.py", line 158, in get_app_config
return self.app_configs[app_label]
KeyError: 'account'
During handling of the above exception, another exception occurred:
[...]
/my/path/basic-django-ecommerce/venv/lib/python3.10/site-packages/django/apps/registry.py", line 165, in get_app_config
raise LookupError(message)
LookupError: No installed app with label 'account'.
During handling of the above exception, another exception occurred:
[...]
/my/path/basic-django-ecommerce/venv/lib/python3.10/site-packages/django/contrib/auth/__init__.py", line 176, in get_user_model
raise ImproperlyConfigured(django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'account.User' that has not been installed
The last message makes me think that I missed some configurations for account.User
, however, it is correctly installed in mainapp_ecommerce/settings.py
INSTALLED_APPS = [
...
'accounts',
...
'django.contrib.auth',
...
]
AUTH_USER_MODEL = 'account.User'
# it changes the builtin model user to User custom defined model
and about that AUTH_USER_MODEL
, it refers to the account.User modeldefined in accounts/models.py
as
class User(AbstractBaseUser):
email = models.EmailField(max_length=255, unique=True)
# full_name = models.CharField(max_length=255, blank=True, null=True)
active = models.BooleanField(default=True) # is h* allowed to login?
staff = models.BooleanField(default=False) # staff user, not superuser
admin = models.BooleanField(default=False) # superuser
timestamp = models.DateTimeField(auto_now_add=True) # superuser
USERNAME_FIELD = 'username'
# email and password are required
REQUIRED_FIELDS = []
objects = UserManager
So, what could be the problem here?
Solution
As indicated by @willeM_Van Onsem, it turns out that
AUTH_USER_MODEL refers to model 'account.User' that has not been installed
because it's not account
, but accounts
.
So I substituted
AUTH_USER_MODEL = 'account.User'
with
AUTH_USER_MODEL = 'accounts.User'
Also, in class User
,
class User(AbstractBaseUser):
email = models.EmailField(max_length=255, unique=True)
# full_name = models.CharField(max_length=255, blank=True, null=True)
active = models.BooleanField(default=True) # is h* allowed to login?
staff = models.BooleanField(default=False) # staff user, not superuser
admin = models.BooleanField(default=False) # superuser
timestamp = models.DateTimeField(auto_now_add=True) # superuser
this line is referring to a non-existing field username
USERNAME_FIELD = 'username'
So I turned the line into
USERNAME_FIELD = 'email'
Furthermore, as indicated in this thread, I have changed
objects = UserManager
to
objects = UserManager()
otherwise one would get the error
self.UserModel._default_manager.db_manager(database).get_by_natural_key(
AttributeError: 'Manager' object has no attribute 'get_by_natural_key'
Then I have run python manage.py makemigrations
, manually inserted a default value timezone.now
when asked by django
It is impossible to add the field 'timestamp' with 'auto_now_add=True' to user without providing a default. This is because the database needs something to populate existing rows.
1) Provide a one-off default now which will be set on all existing rows
2) Quit and manually define a default value in models.py.
Select an option: 1
Please enter the default value as valid Python.
Accept the default 'timezone.now' by pressing 'Enter' or provide another value.
The datetime and django.utils.timezone modules are available, so it is possible to provide e.g. timezone.now as a value.
Type 'exit' to exit this prompt
[default: timezone.now] >>> timezone.now
so that the migration went fine
Migrations for 'accounts':
accounts/migrations/0004_user_timestamp_profile.py
- Add field timestamp to user
- Create model Profile
and in the end I run
python manage.py migrate
Answered By - Tms91
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.