from django.db import models from django.contrib.auth.models import AbstractBaseUser,BaseUserManager from django.utils.translation import ugettext_lazy as _ class MyUserManager(BaseUserManager): def create_user(self, email, student=True, password=None,is_active=False): """ Creates and saves a User with the given email, date of birth and password. """ if not email: raise ValueError(_('Users must have an email address')) user = self.model( email=self.normalize_email(email), student = student ) user.set_password(password) user.is_active = is_active user.save(using=self._db) return user def create_superuser(self, email, date_of_birth, password): """ Creates and saves a superuser with the given email, date of birth and password. """ user = self.create_user(email, password=password, student=False ) user.is_admin = True user.save(using=self._db) return user class MyUser(AbstractBaseUser): class Meta: verbose_name = _('user') verbose_name_plural = _('users') abstract = False email = models.EmailField(unique=True) is_active = models.BooleanField(default=False) is_admin = models.BooleanField(default=False) student = models.BooleanField(default=True) USERNAME_FIELD = 'email' REQUIRED_FIELD='student' objects = MyUserManager() 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 def has_perm(self, perm, obj=None): "Does the user have a specific permission?" # Simplest possible answer: Yes, always return True def has_module_perms(self, app_label): "Does the user have permissions to view the app `app_label`?" # Simplest possible answer: Yes, always return True @property def is_staff(self): "Is the user a member of staff?" # Simplest possible answer: All admins are staff return self.is_admin
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question