问题
I create custom User
models inheriting AbstractUser
in Django
:
class ChachaUser(AbstractUser):
birth = models.DateField()
gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
and my 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')
But I want create superuser by python manage.py createsuperuser
, I have to implement CustomUserManager
, too.
I googled it but there aren't any examples. Any idea or example please?
回答1:
You're interested in UserManager (code).
Example:
from django.contrib.auth.models import AbstractUser, UserManager
class CustomUserManager(UserManager):
# your methods
class CustomUser(AbstractUser):
# fields
objects = CustomUserManager()
回答2:
Or this way:
class CustomUser(User):
objects = MyManager()
class Meta(object):
proxy=True
来源:https://stackoverflow.com/questions/39096052/how-can-i-implement-custom-user-manager-in-django