from django.contrib.auth.models import (
    AbstractBaseUser, PermissionsMixin, UserManager as BaseUserManager,
)
from django.db import models


class UserManager(BaseUserManager):
    def _create_user(self, username, email, password,
                     is_staff, is_superuser, **extra_fields):
        """
        Creates and saves a User with the given email and password.
        """
        email = self.normalize_email(email)
        user = self.model(email=email, is_staff=is_staff,
                          is_superuser=is_superuser, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, username, email=None, password=None, **extra_fields):
        return self._create_user(username, email, password, False, False, **extra_fields)

    def create_superuser(self, username, email, password, **extra_fields):
        return self._create_user(username, email, password, True, True, **extra_fields)


class User(AbstractBaseUser, PermissionsMixin):
    """
    Custom User model inheriting from AbstractBaseUser. Should be admin site
    compatible.

    Email and password are required. Other fields are optional.
    """
    email = models.EmailField(blank=True, unique=True)
    is_staff = models.BooleanField(default=False)

    objects = UserManager()

    USERNAME_FIELD = 'email'

    def get_short_name(self):
        return self.email.split('@')[0]

@@ -1,20 +1,53 @@
+from django.conf import settings%0A
 from django.contrib.
@@ -171,17 +171,187 @@
 models%0A%0A
-%0A
+# Only define these classes when we're testing a custom user model. Otherwise%0A# we'll get SystemCheckError %22fields.E304%22.%0Aif settings.AUTH_USER_MODEL == %22tests.User%22:%0A    
 class Us
@@ -378,16 +378,20 @@
 nager):%0A
+    
     def 
@@ -453,24 +453,28 @@
             
+    
 is_staff, is
@@ -502,36 +502,44 @@
 ields):%0A        
-%22%22%22%0A
+    %22%22%22%0A    
         Creates 
@@ -590,32 +590,36 @@
 ssword.%0A        
+    
 %22%22%22%0A        emai
@@ -610,24 +610,28 @@
 %22%22%22%0A        
+  
+  
 email = self
@@ -654,16 +654,20 @@
 (email)%0A
+    
         
@@ -738,24 +738,28 @@
             
+   
+ 
 is_superuser
@@ -793,24 +793,28 @@
 ds)%0A        
+    
 user.set_pas
@@ -833,24 +833,28 @@
 rd)%0A        
+   
+ 
 user.save(us
@@ -871,24 +871,28 @@
 db)%0A        
+    
 return user%0A
@@ -892,16 +892,20 @@
 n user%0A%0A
+    
     def 
@@ -976,32 +976,36 @@
 ields):%0A        
+    
 return self._cre
@@ -1071,16 +1071,20 @@
 ields)%0A%0A
+    
     def 
@@ -1150,32 +1150,36 @@
 ields):%0A        
+ 
+   
 return self._cre
@@ -1244,16 +1244,20 @@
 elds)%0A%0A%0A
+    
 class Us
@@ -1296,28 +1296,36 @@
 Mixin):%0A    
+  
+  
 %22%22%22%0A
+    
     Custom U
@@ -1393,16 +1393,20 @@
 ite%0A    
+  
+  
 compatib
@@ -1410,16 +1410,20 @@
 tible.%0A%0A
+    
     Emai
@@ -1478,28 +1478,36 @@
 tional.%0A    
+   
+ 
 %22%22%22%0A
+    
     email = 
@@ -1549,24 +1549,28 @@
 e=True)%0A    
+   
+ 
 is_staff = m
@@ -1604,16 +1604,20 @@
 False)%0A%0A
+    
     obje
@@ -1637,16 +1637,20 @@
 ager()%0A%0A
+    
     USER
@@ -1671,16 +1671,20 @@
 email'%0A%0A
+    
     def 
@@ -1705,16 +1705,20 @@
 (self):%0A
+    
         

Fix for SystemCheckError field.E304