3

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']
aaron
  • 39,695
  • 6
  • 46
  • 102
Tom
  • 364
  • 2
  • 19
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/230321/discussion-on-question-by-tom-django-how-to-use-auth-user-model). – Samuel Liew Mar 24 '21 at 13:39

3 Answers3

3

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']
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • I just updated my question. You helped me get the answer, so thank you! Any idea why it is now saying my SECRET_KEY is missing? – Tom Mar 24 '21 at 06:56
  • I am still having this issue. I will pay you $50 /hour to help me figure this out, I feel like we were close to solving it. For updated details I made this stackoverflow post: https://stackoverflow.com/questions/66791148/auth-user-model-error-in-production-only-auth-user-model-refers-to-model-auth – Tom Mar 25 '21 at 03:25
2

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.

Abdul Aziz Barkat
  • 19,475
  • 3
  • 20
  • 33
0

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)
ZeevhY Org.
  • 295
  • 3
  • 8
  • The documentation explicitly says that this is not a good idea: https://docs.djangoproject.com/en/dev/topics/auth/customizing/#referencing-the-user-model The reason that this often is a problem is that if you later change the user model, it will take some time to alter all `ForeignKey`s/`OneToOneField`s. – Willem Van Onsem Mar 24 '21 at 07:20