Previously, I failed to setup the AUTH_USER_MODEL = 'apps.authuser.User'
for my project. All my apps is combined in one folder, called with apps
. the project structure is like this;
├── apps
│ ├── authuser/
│ │ ├── __init__.py
│ │ ├── apps.py
│ │ ├── models/
│ │ │ ├── __init__.py
│ │ │ └── user.py
│ │ ├── utils
│ │ │ ├── backends.py
│ │ │ └── __init__.py
│ ├── blog/
│ │ ├── __init__.py
│ │ ├── apps.py
│ │ └── models/
│ ├── product/
│ │ ├── __init__.py
│ │ └── apps.py
├── core/
│ ├── asgi.py
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── templates/
├── db.sqlite3
└── manage.py
1. settings.py
;
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
....
# major apps
'apps.authuser',
'apps.blog',
'apps.product',
]
# Custom Auth User Model
AUTH_USER_MODEL = 'apps.authuser.User'
AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
'apps.authuser.utils.backends.CustomAuthBackend'
]
2. apps/authuser/apps.py
;
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.apps import AppConfig
from django.utils.translation import ugettext_lazy as _
class AuthUserConfig(AppConfig):
name = 'apps.authuser'
verbose_name = _('App Auth User')
3. apps/authuser/__init__.py
;
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
default_app_config = 'apps.authuser.apps.AuthUserConfig'
4. apps/authuser/models/__init__.py
;
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
# to custom the Auth User Model
# you need to import that model into this __init__.py file
# because we use the folder to manage it models.py
from .user import User
5. apps/authuser/models/user.py
;
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
from django.urls import reverse
from django.conf import settings
from django.contrib.auth.models import AbstractUser
from django.utils.translation import ugettext_lazy as _
class User(AbstractUser):
"""
extending from base `AbstractUser`
to rewrite the `id` AutoField to `id` `BigAutoField`
"""
id = models.BigAutoField(primary_key=True)
@property
def fullname(self):
if hasattr(self, 'profile') and self.profile:
return self.profile.display_name
return self.get_full_name() or self.username
I also tried with this solution https://stackoverflow.com/a/50179553/6396981, but still doesn't work.
(env-mysite) ➜ mysite git:(development) ✗ ./manage.py runserver
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/home/binpy/envs/env-mysite/lib/python3.7/site-packages/django/db/models/utils.py", line 11, in make_model_tuple
app_label, model_name = model.split(".")
ValueError: too many values to unpack (expected 2)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner
self.run()
File "/usr/lib/python3.7/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/home/binpy/envs/env-mysite/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/home/binpy/envs/env-mysite/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 110, in inner_run
autoreload.raise_last_exception()
File "/home/binpy/envs/env-mysite/lib/python3.7/site-packages/django/utils/autoreload.py", line 76, in raise_last_exception
raise _exception[1]
File "/home/binpy/envs/env-mysite/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
autoreload.check_errors(django.setup)()
File "/home/binpy/envs/env-mysite/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/home/binpy/envs/env-mysite/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/binpy/envs/env-mysite/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/home/binpy/envs/env-mysite/lib/python3.7/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/home/binpy/envs/env-mysite/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/binpy/envs/env-mysite/lib/python3.7/site-packages/django/contrib/admin/models.py", line 39, in <module>
class LogEntry(models.Model):
File "/home/binpy/envs/env-mysite/lib/python3.7/site-packages/django/db/models/base.py", line 161, in __new__
new_class.add_to_class(obj_name, obj)
File "/home/binpy/envs/env-mysite/lib/python3.7/site-packages/django/db/models/base.py", line 326, in add_to_class
value.contribute_to_class(cls, name)
File "/home/binpy/envs/env-mysite/lib/python3.7/site-packages/django/db/models/fields/related.py", line 747, in contribute_to_class
super().contribute_to_class(cls, name, private_only=private_only, **kwargs)
File "/home/binpy/envs/env-mysite/lib/python3.7/site-packages/django/db/models/fields/related.py", line 318, in contribute_to_class
lazy_related_operation(resolve_related_class, cls, self.remote_field.model, field=self)
File "/home/binpy/envs/env-mysite/lib/python3.7/site-packages/django/db/models/fields/related.py", line 80, in lazy_related_operation
return apps.lazy_model_operation(partial(function, **kwargs), *model_keys)
File "/home/binpy/envs/env-mysite/lib/python3.7/site-packages/django/db/models/fields/related.py", line 78, in <genexpr>
model_keys = (make_model_tuple(m) for m in models)
File "/home/binpy/envs/env-mysite/lib/python3.7/site-packages/django/db/models/utils.py", line 20, in make_model_tuple
"must be of the form 'app_label.ModelName'." % model
ValueError: Invalid model reference 'apps.authuser.User'. String model references must be of the form 'app_label.ModelName'.
When I tried with AUTH_USER_MODEL = 'authuser.User'
, it showed me an error AttributeError: 'Settings' object has no attribute 'authuser_MODEL'
.
Traceback (most recent call last):
File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner
self.run()
File "/usr/lib/python3.7/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/home/binpy/envs/env-mysite/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/home/binpy/envs/env-mysite/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
self.check_migrations()
File "/home/binpy/envs/env-mysite/lib/python3.7/site-packages/django/core/management/base.py", line 459, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "/home/binpy/envs/env-mysite/lib/python3.7/site-packages/django/db/migrations/executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "/home/binpy/envs/env-mysite/lib/python3.7/site-packages/django/db/migrations/loader.py", line 53, in __init__
self.build_graph()
File "/home/binpy/envs/env-mysite/lib/python3.7/site-packages/django/db/migrations/loader.py", line 210, in build_graph
self.load_disk()
File "/home/binpy/envs/env-mysite/lib/python3.7/site-packages/django/db/migrations/loader.py", line 112, in load_disk
migration_module = import_module(migration_path)
File "/home/binpy/envs/env-mysite/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/binpy/envs/env-mysite/python.web.id/apps/blog/migrations/0001_initial.py", line 9, in <module>
class Migration(migrations.Migration):
File "/home/binpy/envs/env-mysite/python.web.id/apps/blog/migrations/0001_initial.py", line 14, in Migration
migrations.swappable_dependency(settings.authuser_MODEL),
File "/home/binpy/envs/env-mysite/lib/python3.7/site-packages/django/conf/__init__.py", line 84, in __getattr__
val = getattr(self._wrapped, name)
AttributeError: 'Settings' object has no attribute 'authuser_MODEL'
and when I tried to add label = 'apps.authuser'
in my AuthUserConfig
, it showed me this error;
ValueError: Invalid model reference 'apps.authuser.User_groups'.
String model references must be of the form 'app_label.ModelName'.
As the error says. You just need the app name and the model name.
String model references must be of the form 'app_label.ModelName'.
So the auth model user should be
AUTH_USER_MODEL = 'authuser.User'