Home > database >  django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'authentication.Us
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'authentication.Us

Time:07-18

I'm creating app with authentication. My project is dockerized. When I run the server everything works fine, except

authentication.User: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.

But when I want to run docker-compose exec web python3 manage.py makemigrations or docker-compose exec web python3 manage.py migrate I get an error:

File "/usr/local/lib/python3.9/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 'authentication.User' that has not been installed

I've thought it points to settings.py field AUTH_USER_MODEL, but I haven't got it.

My views.py:

def signup(request):
    if request.method == "POST":
        context = {'has_error': False, 'data': request.POST}
        email = request.POST.get('email')
        username = request.POST.get('username')
        password = request.POST.get('password')

        if len(password) < 6:
            messages.add_message(request, messages.ERROR,
                                 'Password should be at least 6 characters')
            context['has_error'] = True

        if not validate_email(email):
            messages.add_message(request, messages.ERROR,
                                 'Enter a valid email address')
            context['has_error'] = True

        if not username:
            messages.add_message(request, messages.ERROR,
                                 'Username is required')
            context['has_error'] = True

        if models.User.objects.filter(username=username).exists():
            messages.add_message(request, messages.ERROR,
                                 'Username is taken, choose another one')
            context['has_error'] = True

            return render(request, 'authentication/signup.html', context)    # status=409

        if models.User.objects.filter(email=email).exists():
            messages.add_message(request, messages.ERROR,
                                 'Email is taken, choose another one')
            context['has_error'] = True

            return render(request, 'authentication/signup.html', context)   # status=409

        if context['has_error']:
            return render(request, 'authentication/signup.html', context)

        user = models.User.objects.create_user(username=username, email=email)
        user.set_password(password)
        user.save()

    return render(request, 'authentication/signup.html')

My models.py:

from django.db import models



class User(models.Model):
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
    )

    username = models.CharField(
        max_length=200
    )
    def __str__(self):
        return self.email

My signup.html:

{% include  "_base.html" %}
{% load static %}

{% block title %}Sign Up{% endblock title %}

{% block content %}
<link rel="stylesheet" href="{% static 'css/authentication/signup.css' %}">
<div >
  <form  method="post" action="{% url 'signup' %}">
    {% csrf_token %}
    <input type="text" placeholder="Email"  name="email">
    <input type="text" placeholder="Username"  name="username">
    <input type="text" placeholder="Password"  name="password">
    <button type="submit" >Sign Up</button>
  </form>
</div>
{% endblock content %}

_base.html is just navbar. When I add AUTH_USER_MODEL to settings.py it results in same error.

CodePudding user response:

For this you should try adding an id field in the user model like so

from uuid import uuid4
id = models.UUIDField(primary_key=True, editable=False, default=uuid4)

Also in your user model add this to the class

from django.contrib.auth.models import AbstractUser
class CustomUser(AbstractUser):

I advice you also change the class name form User to CustomUser to avoid clashes with internal django backend, that will be.

From

class User(models.Model):

to

class CustomUser(AbstractUser):

Hope this works

  • Related