2017-02-22 45 views
1

我對Django有點新鮮。我創建了一個自定義的用戶模型,因爲我想用於身份驗證的電子郵件,並隨後在其運作良好的文檔的例子,有以下代碼:向Django用戶模型添加更多字段的最佳方法是什麼?

class CustomUserManager(BaseUserManager): 

    def _create_user(self, email, password, 
       is_staff, is_superuser, **extra_fields): 
     """ 
     Creates and saves a User with the given email and password. 
     """ 
     now = timezone.now() 
     if not email: 
      raise ValueError('The given email must be set') 
     email = self.normalize_email(email) 
     user = self.model(email=email, 
         is_staff=is_staff, is_active=True, 
         is_superuser=is_superuser, last_login=now, 
         date_joined=now, **extra_fields) 
     user.set_password(password) 
     user.save(using=self._db) 
     return user 

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

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


class User (AbstractBaseUser, PermissionsMixin): 
    ''' 
    Custom User Model 
    ''' 
    email = models.EmailField(verbose_name = "email address", max_length = 255, unique = True) 
    date_joined = models.DateTimeField('date joined', default=timezone.now) 
    first_name = models.CharField(_('first name'), max_length=30, blank=False) 
    last_name = models.CharField(_('last name'), max_length=30, blank=False) 
    is_active = models.BooleanField(default=True) 
    is_admin  = models.BooleanField(default=False) 
    is_staff  = models.BooleanField(default=False) 

    USERNAME_FIELD = "email" 
    REQUIRED_FIELDS = [] 

    objects = CustomUserManager() 

    def get_absolute_url(self): 
     return "https://stackoverflow.com/users/%s/" % urlquote(self.email) 

    def get_full_name(self): 
     """ 
     Returns the first_name plus the last_name, with a space in between. 
     """ 
     full_name = '%s %s' % (self.first_name, self.last_name) 
     return full_name.strip() 

    def get_short_name(self): 
     "Returns the short name for the user." 
     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]) 

我唯一的問題是現在我想添加更多的信息到我的自定義用戶模型,如可用性日期,個人資料圖片,費用等(我正在做一個迷你linkedin網站)。我覺得爲用戶模型添加大量額外的字段有點麻煩,並且隨着時間的推移,字段數量將會增長。有一個更好的方法嗎?

感謝

+0

請修復您的代碼的縮進。我們目前無法正確讀取它。 – Chris

+0

@Chris修復了代碼縮進。對不起 –

回答

1

沒有理由不更加字段添加到這個模型爲你描述。這正是此自定義用戶模型和模型遷移的用處。

如果你正在尋找創建爲不同的用戶不同的用戶配置文件,您可以使用multi table inheritance,就像這樣:

class User (AbstractBaseUser, PermissionsMixin): 
    ''' 
    Custom User Model 
    ''' 
    ...fields that are common to CreativeUser and ClientUser 

class CreativeUser(User): 
    # Fields unique to CreativeUser 

class ClientUser(User): 
    # Fields unique to ClientUser 

現在,您可以查詢使用User.objects.all()的所有用戶,您可以查詢CreativeUser.objects.all(),或ClientUser.objects.all() 。請注意,User.objects.all()將不包含任何其他兩個表中唯一的字段。

+0

感謝您的回答。我想我擔心的是會有兩種類型的用戶:CreativeUser和ClientUser,它們將共享常見的用戶特徵。兩種類型的用戶都會持有不同的東西,並會通過單獨的註冊表單來捕獲這兩種類型的獨特信息。我不確定是否最好爲具有OneToOneField映射的每種類型的用戶創建UserProfile模型,或者僅創建User的子類。 –

+0

@FrancisN更新了我的答案 – YPCrumble

+0

這工作!我使用CreativeUser作爲RegistrationForm的表單模型,以及我想要顯示的額外字段並與用戶一起保存。非常感謝?古突士! –

相關問題