自定义用户模型:生产准备还需要什么?

时间:2013-08-06 13:14:12

标签: production django-1.5

我是Django的新手,需要使用电子邮件地址作为我的应用的用户名 - 并添加一些自定义字段。所以我关注the full example in django docs来制作自定义用户模型。

我非常担心示例中的部分说:

  

此示例说明了大多数组件如何协同工作,   但不打算直接复制到生产项目中   使用

我不确定还需要什么才能使用户模型生产准备就绪。

该示例是否缺少一些重要的安全功能? (我希望我的自定义模型和身份验证与默认的Django一样好。)

有没有人有一个生产就绪的例子?

1 个答案:

答案 0 :(得分:0)

最后,我所做的是关注the full example in the django website,并将我的代码与默认的Django用户模型进行比较。我相信默认的Django用户模型是生产就绪的,实际上,唯一的区别是自定义用户模型是使用电子邮件作为用户名,因此我感到非常放心,我的自定义用户模型已准备就绪。

这是我的代码:

在models.py

from django.contrib.auth.models import (
    BaseUserManager, AbstractBaseUser
)
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from django.core.mail import send_mail

class MyUserManager(BaseUserManager):
    def create_user(self, 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),
        )

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, password):
        """
        Creates and saves a superuser with the given email and password.
        """
        user = self.create_user(email,
            password=password
        )
        user.is_admin = True
        user.save(using=self._db)
        return user

class MyUser(AbstractBaseUser):
    first_name = models.CharField(_('first name'), max_length=30, blank=True)
    last_name = models.CharField(_('last name'), max_length=30, blank=True)
    email = models.EmailField(_('email address'), max_length=254, blank=False, unique=True, db_index=True)  
    is_active = models.BooleanField(_('active'), default=True,
        help_text=_('Designates whether this user should be treated as '
                    'active. Unselect this instead of deleting accounts.'))
    is_admin = models.BooleanField(default=False)
    date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
    gender_choices = (
        ('M', 'Male'),
        ('F', 'Female'),
        )
    gender = models.CharField(max_length=1, choices=gender_choices, blank=True)
    date_of_birth = models.DateField(null=True, blank=True)
    city = models.CharField(max_length=50, blank=True)
    phone_number = models.CharField(max_length=15, blank=True)
    description = models.TextField(blank=True)
    work = models.TextField(blank=True)

    objects = MyUserManager()

    USERNAME_FIELD = 'email'

    class Meta:
        verbose_name = _('user')
        verbose_name_plural = _('users')

    def get_full_name(self):
        full_name = '%s %s' % (self.first_name, self.last_name)
        return full_name.strip()

    def get_short_name(self):
        return self.first_name

    def email_user(self, subject, message, from_email=None):
        """
        Sends an email to this User.
        """
        send_mail(subject, message, from_email, [self.email])

    def __unicode__(self):
        return self.first_name + ' ' + self.last_name

    def has_perm(self, perm, obj=None):
        return True

    def has_module_perms(self, app_label):
        return True

    @property
    def is_staff(self):
        return self.is_admin

同样在我的forms.py和views.py中,我使用了User模型,因此我添加了以下内容:

from django.contrib.auth import get_user_model
User = get_user_model()