Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manager isn't available; 'auth.User' has been swapped for 'polls.User'

Tags:

python

django

I have created a different version of the User model where there is no username field and I am now trying to implement a sign in feature, but I keep getting the error

Manager isn't available; 'auth.User' has been swapped for 'polls.User'

I have searched the rest of this site and tried to implement the feature beneath to fix the problem but to no avail.

from django.contrib.auth import get_user_model

User = get_user_model()

Here is the rest of my files

Views

    from django.http import HttpResponse
    from django.shortcuts import get_object_or_404, render, render_to_response, redirect
    from django.contrib.auth.decorators import login_required
    from django.contrib.auth import login, authenticate
    from django.shortcuts import render, redirect

    from polls.forms import SignUpForm
    from django.contrib.auth import get_user_model

    User = get_user_model()

    @login_required
    def home(request):
        return render(request, 'home.html')

    def signup(request):
        if request.method == 'POST':
            form = SignUpForm(request.POST)
            if form.is_valid():
                form.save()
                username = None
                raw_password = form.cleaned_data.get('password1')
                user = authenticate(password=raw_password)
                login(request, user)
                return redirect('home')
        else:
            form = SignUpForm()
        return render(request, 'signup.html', {'form': form})

forms

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User


class SignUpForm(UserCreationForm):
    first_name = forms.CharField(max_length=30, required=False, help_text='Optional.')
    last_name = forms.CharField(max_length=30, required=False, help_text='Optional.')
    email = forms.EmailField(max_length=254, help_text='Required. Inform a valid email address.')
    username = None

    class Meta:
        model = User
        fields = ('first_name', 'last_name', 'email', 'password1', 'password2', )

models

from django.db import models
from django.contrib.auth.models import AbstractUser, BaseUserManager
from django.db import models
from django.utils.translation import ugettext_lazy as _

class UserManager(BaseUserManager):
    """Define a model manager for User model with no username field."""

    use_in_migrations = True

    def _create_user(self, email, password, **extra_fields):
        """Create and save a User with the given email and password."""
        if not email:
            raise ValueError('The given email must be set')
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, email, password=None, **extra_fields):
        """Create and save a regular User with the given email and password."""
        extra_fields.setdefault('is_staff', False)
        extra_fields.setdefault('is_superuser', False)
        return self._create_user(email, password, **extra_fields)

    def create_superuser(self, email, password, **extra_fields):
        """Create and save a SuperUser with the given email and password."""
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError('Superuser must have is_staff=True.')
        if extra_fields.get('is_superuser') is not True:
            raise ValueError('Superuser must have is_superuser=True.')

        return self._create_user(email, password, **extra_fields)


class User(AbstractUser):
    """User model."""

    username = None
    email = models.EmailField(_('email address'), unique=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = UserManager()

If anyone could provide any clarity on this it would be greatly appreciated

like image 832
Ethan Pearce Avatar asked Feb 16 '18 09:02

Ethan Pearce


2 Answers

In your forms.py you have to also change

from django.contrib.auth.models import User

to

from django.contrib.auth import get_user_model

User = get_user_model()

This goes for everywhere you use User. The traceback of the error will tell you exactly where the error was caused. When seeking help debugging, always include the traceback in the question.

like image 79
Håken Lid Avatar answered Sep 29 '22 16:09

Håken Lid


Edited: Never mind, see Håken Lid answer. I misread the problem.

But, nonetheless you have to fix the code below as well


Depends on what Django version you are using, but the problem is in this line:

user = authenticate(password=raw_password)

You should pass also the username:

user = authenticate(username='john', password='secret')

Then, then authenticate method will return either a User instance or None.

if user is not None:
    login(request, user)
else:
    # authenticated failed
like image 37
Vitor Freitas Avatar answered Sep 29 '22 18:09

Vitor Freitas