1

I'm using Django 1.7 and am trying to authenticate a user with email instead of the provided Django auth user.

This is my models.py

from django.db import models from django.contrib.auth.models import AbstractBaseUser, BaseUserManager class MyUserManager(BaseUserManager): def create_user(self, email, password=None): if not email: raise ValueError('Users must have an email address') user = self.model( email=MyUserManager.normalize_email(email), ) user.set_password(password) user.save(using=self._db) return user def create_superuser(self, email, password): user = self.create_user(email, password=password, ) user.is_admin = True user.save(using=self._db) return user class MyUser(AbstractBaseUser): """ Custom user class. """ email = models.EmailField('email address', unique=True, db_index=True) joined = models.DateTimeField(auto_now_add=True) is_active = models.BooleanField(default=True) is_admin = models.BooleanField(default=False) USERNAME_FIELD = 'email' def __unicode__(self): return self.email 

and this is a snippet from my views.py

def auth_view(request): username = request.POST.get('username', '') password = request.POST.get('password', '') user = auth.authenticate(username=username, password=password) if user is not None: auth.login(request, user) return HttpResponseRedirect('/') else: return HttpResponseRedirect('/invalid/') def register_user(request): if request.method == 'POST': form = MyRegistrationForm(request.POST) if form.is_valid(): print "Form is valid" form.save() return HttpResponseRedirect('/register_success/') args = {} args.update(csrf(request)) args['form'] = MyRegistrationForm() return render_to_response('register.html', args, context_instance=RequestContext(request)) 

and finally, my forms.py

from django import forms from django.contrib.auth.models import User class MyRegistrationForm(forms.ModelForm): """ Form for registering a new account. """ email = forms.EmailField(widget=forms.EmailInput,label="Email") password1 = forms.CharField(widget=forms.PasswordInput, label="Password") password2 = forms.CharField(widget=forms.PasswordInput, label="Password (again)") class Meta: model = User fields = ['email', 'password1', 'password2'] def clean(self): """ Verifies that the values entered into the password fields match NOTE: Errors here will appear in ``non_field_errors()`` because it applies to more than one field. """ cleaned_data = super(MyRegistrationForm, self).clean() if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data: if self.cleaned_data['password1'] != self.cleaned_data['password2']: raise forms.ValidationError("Passwords don't match. Please enter both fields again.") return self.cleaned_data def save(self, commit=True): user = super(MyRegistrationForm, self).save(commit=False) user.set_password(self.cleaned_data['password1']) if commit: user.save() return user 

Whenever I try to register an account, I get an error 'NoneType' object has no attribute '_insert' from forms.py calling user.save and views.py calling form.save. I don't really know how to write the user.save, but I'd imagine that would fix both errors.

Can anyone help me?

1 Answer 1

2

look at forms.py imports

from django.contrib.auth.models import User 

must import MyUser instead of that

same in

class Meta: model = User fields = ['email', 'password1', 'password2'] 

and add to MyUser class

objects = MyUserManage() 

change to

class Meta: model = MyUser fields = ['email', 'password1', 'password2'] 

and settings.py must set:

AUTH_USER_MODEL = '<apppath>.MyUser' 
Sign up to request clarification or add additional context in comments.

2 Comments

MyUserManage isnt link to MyUser, add this to MyUser class: objects = MyUserManager()
I forgot to add the objects = MyUserManager(), so it works now. Thank you so much!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.