創建自定義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
了。
我使用了它,但沒有任何例子。有什麼想法或例子嗎?
首先代替進口'ChachaUser'使用'get_user_model()'無處不在。如果你需要添加字段'gender,birth'到'createsuperuser'命令,使用 'REQUIRED_FIELDS = ['email','birth','gender']' –
@AnnaVracheva:它工作的很好!謝謝 :) – user3595632