Reputation: 414
I have seen several posts and read the documentation about how it's best practice to set AUTH_USER_MODEL
in settings.py, but do not see any actual examples on how to do that. I have tried several configurations but keep getting this error:
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'auth.User' that has not been installed
It doesn't even tell me where the error is occurring. Below are the methods I have tried:
METHOD 1:
from django.contrib.auth import get_user_model
User = get_user_model()
Then I would just reference my user like this:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
METHOD 2:
from django.contrib.auth.models import User
Neither worked, and I'm not sure how I would set AUTH_USER_MODEL
in settings.py if I just want to use the standard user model. I'm not customizing the User object at all. I assume something like AUTH_USER_MODEL = 'django.contrib.auth.models.User'
but I'm not sure.
Now I'm getting this:
AttributeError: 'str' object has no attribute '_meta'
users/forms.py
from django import forms
from django.conf import settings
from django.contrib.auth.forms import UserCreationForm
from .models import Profile
class UserRegisterForm(UserCreationForm):
email = forms.EmailField()
class Meta:
model = settings.AUTH_USER_MODEL
fields = ['username', 'email', 'password1', 'password2']
class UserUpdateForm(forms.ModelForm):
email = forms.EmailField()
class Meta:
model = settings.AUTH_USER_MODEL
fields = ['username', 'email']
Upvotes: 6
Views: 11711
Reputation: 477170
I assume something like
AUTH_USER_MODEL = 'django.contrib.auth.models.User'
but i'm not sure.
You refer to a model with app_name.ModelName
, so in this case that is:
# settings.py
AUTH_USER_MODEL = 'auth.User'
This is also the default value, so if you want to work with Django's user model, you can simply omit the AUTH_USER_MODEL
setting in the settings.py
.
furthermore you need to add django.contrib.auth
to the INSTALLED_APPS
, so:
# settings.py
INSTALLED_APPS = [
# …,
'django.contrib.auth',
# …,
]
In a ModelForm
, ModelSerializer
, etc. you work with get_user_model()
to get a reference to the user model class:
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import get_user_model
from .models import Profile
User = get_user_model()
class UserRegisterForm(UserCreationForm):
email = forms.EmailField()
class Meta:
model = User
fields = ['username', 'email', 'password1', 'password2']
class UserUpdateForm(forms.ModelForm):
email = forms.EmailField()
class Meta:
model = User
fields = ['username', 'email']
Upvotes: 5
Reputation: 19
I agree with Willem in his case. If you are going to keep this, I recommend the following.
In your Method1,
from django.contrib.auth import get_user_model
User = get_user_model()
and then in views.py, use get_user_model()
instead of User
.
I hope it will work in your case.
Upvotes: 1
Reputation: 365
You could simply do this.
Models.py
from django.contrib.auth.models import User
class Profile(models.Model):
user = models.OneToOneField(User , on_delete=models.DO_WhatEver_You_Want)
Upvotes: 0