title: Can't authenticate Django user in shell
body: <p>For some reason I can't authenticate a newly created user in the Django shell and I really don't know what's wrong. Must be something very stupid...</p>
  
  <p>Start Django shell:</p>
  
  <pre><code>(venv) xxx:xxx xxx$ python ../manage.py shell
  Python 3.6.0 (v3.6.0, ...) 
  [GCC 4.2.1 (...) (dot 3)] on darwin
  Type "help", "copyright", "credits" or "license" for more information.
  (InteractiveConsole)
  </code></pre>
  
  <p>Create a new user:</p>
  
  <pre><code>&gt;&gt;&gt; from django.contrib.auth import get_user_model
  &gt;&gt;&gt; email = "testemail@gmail.com"
  &gt;&gt;&gt; password = "testpassword"
  &gt;&gt;&gt; user = get_user_model().objects.create_user("TestUser", email, password)
  </code></pre>
  
  <p>The new user seems to have been added to the database correctly:</p>
  
  <pre><code>&gt;&gt;&gt; get_user_model().objects.all()
  &lt;QuerySet [&lt;MyUser: testemail@gmail.com&gt;]&gt;
  &gt;&gt;&gt; u = get_user_model().objects.first()
  &gt;&gt;&gt; u.username
  'TestUser'
  &gt;&gt;&gt; u.email
  'testemail@gmail.com'
  &gt;&gt;&gt; u.password
  'pbkdf2_sha256$36000$Onrvxuy09b6r$sCuz/2/bIbeg5j7cfO934kCIvzVdxqo1s1v6x6nwYLY='
  </code></pre>
  
  <p>But authentication fails:</p>
  
  <pre><code>&gt;&gt;&gt; from django.contrib.auth import authenticate
  &gt;&gt;&gt; user = authenticate(email = email, password = password)
  &gt;&gt;&gt; user.email
  Traceback (most recent call last):
    File "&lt;console&gt;", line 1, in &lt;module&gt;
  AttributeError: 'NoneType' object has no attribute 'email'
  &gt;&gt;&gt;
  </code></pre>
  
  <p>I have a custom User model hence the use of <code>get_user_model()</code>.</p>
  
  <p>models.py:</p>
  
  <pre><code>from django.db import models
  from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
  
  class MyUserManager(BaseUserManager):
      def create_user(self, username, email, password=None):
          """
          Creates and saves a User with the given email and password.
          """
          if not email:
              raise ValueError('Users must have an email address')
  
          user = self.model(
              email=self.normalize_email(email).lower(),
          )
  
          user.username = username
          user.is_active = False
          user.set_password(password)
          user.save(using=self._db)
          return user
  
          ...
  
  class MyUser(AbstractBaseUser):
      email = models.EmailField(verbose_name='email address', max_length=255, unique=True)
      is_active = models.BooleanField(default=False)
      username = models.CharField(max_length=50, default="Anonymous")
      is_guest = models.BooleanField(default=False)
  
      objects = MyUserManager()
  
      USERNAME_FIELD = 'email'
  
      def get_full_name(self):
          # The user is identified by their email address
          return self.email
  
      def get_short_name(self):
          # The user is identified by their email address
          return self.email
  
      def __str__(self):              # __unicode__ on Python 2
          return self.email
  
      ...
  </code></pre>
  
  <p>Any help would be greatly appreciated!</p>
  
  <p>The below doesn't work either:</p>
  
  <pre><code>&gt;&gt;&gt; user = authenticate(username="TestUser", password=password)
  &gt;&gt;&gt; user.email
  Traceback (most recent call last):
    File "&lt;console&gt;", line 1, in &lt;module&gt;
  AttributeError: 'NoneType' object has no attribute 'email'
  &gt;&gt;&gt; 
  </code></pre>