簡體   English   中英

如何在Django 1.5中自定義用戶模型

[英]How to custom user models in django 1.5

眾所周知,django提供了一個強大的用戶和身份驗證應用程序。

通常,有兩種方法可以自定義用戶

  1. 使用OneToOneField方法。 我們可以定義一個模型配置文件,其中保留一些用戶信息字段。 我們還可以使用get_profile方法獲取配置文件。 但是,有一個問題,如果我們獲得用戶個人資料,就必須加入數據表。 一些代碼演示:

     from django.db import models from django.contrib.auth.models import User class Profile(models.Model): user = models.OneToOneField(User) 
  2. 使用AbstractBaseUser,PermissionsMixin。 我們可以通過更改Django--1.5中的AbstractBaseUser,PermissionsMixin來自定義用戶,並且必須定義UserAdmin。文檔: https : //docs.djangoproject.com/en/1.5/topics/auth/customizing/#extending-the現有用戶模型

我很困惑,不知道哪種方法更好?

自Django 1.5以來,最好的方法就是使用自定義用戶模型 ,將AUTH_USER_MODEL設置為您的用戶模型。

沒有理由感到困惑。 您鏈接到的文檔是正確的:您應該擴展AbstractBaseUser。

另外,您應該升級到1.6版。

您要做的是將AUTH_USER_MODEL添加到具有自定義用戶類路徑的設置中,該路徑擴展了AbstractBaseUser(可定制性更高的版本)或AbstractUser。

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


class MyUserManager(BaseUserManager):
    def create_user(self, email, date_of_birth, password=None):
        """
        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=MyUserManager.normalize_email(email),
            date_of_birth=date_of_birth,
        )

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

    def create_superuser(self, username, date_of_birth, password):
        """
        Creates and saves a superuser with the given email, date of
        birth and password.
        """
        u = self.create_user(username,
                        password=password,
                        date_of_birth=date_of_birth
                    )
        u.is_admin = True
        u.save(using=self._db)
        return u


class MyUser(AbstractBaseUser):
    email = models.EmailField(
                        verbose_name='email address',
                        max_length=255
                    )
    date_of_birth = models.DateField()
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    objects = MyUserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['date_of_birth']

    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 __unicode__(self):
        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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM