How can I implement custom user manager in django?

大憨熊 提交于 2019-12-23 15:49:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!