2016-08-23 77 views
1

創建自定義User車型Django繼承AbstractUser如何在django中實現自定義用戶管理器?

class ChachaUser(AbstractUser): 
    birth = models.DateField() 
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES) 

和我CustomUserCreationForm

GENDER_CHOICES = (
    ('M', '남'), 
    ('F', '여'), 
) 


class MyUserCreationForm(UserCreationForm): 

    birth = forms.DateField(
     widget=forms.SelectDateWidget(
      years=range(1970, 2015) 
     ), 
     required=True, 
    ) 
    gender = forms.ChoiceField(choices=GENDER_CHOICES, initial='M') 

    class Meta(UserCreationForm.Meta): 
     model = ChachaUser 
     fields = UserCreationForm.Meta.fields + ('birth', 'gender') 

但我想通過python manage.py createsuperuser創建超級用戶,我要實現CustomUserManager了。

我使用了它,但沒有任何例子。有什麼想法或例子嗎?

+0

首先代替進口'ChachaUser'使用'get_user_model()'無處不在。如果你需要添加字段'gender,birth'到'createsuperuser'命令,使用 'REQUIRED_FIELDS = ['email','birth','gender']' –

+0

@AnnaVracheva:它工作的很好!謝謝 :) – user3595632

回答

1

您有興趣UserManagercode)。

例子:

from django.contrib.auth.models import AbstractUser, UserManager 

class CustomUserManager(UserManager): 
    # your methods 

class CustomUser(AbstractUser): 
    # fields 

    objects = CustomUserManager() 
0

或者這樣說:

class CustomUser(User): objects = MyManager() class Meta(object): proxy=True

相關問題