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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With