Is it necessary to make a custom manager for the custom User model?

14 views
Skip to first unread message

Benjamin Smith

unread,
Oct 9, 2015, 4:51:25 AM10/9/15
to django...@googlegroups.com
I am making a custom user model using the AbstractBaseUser and PermissionsMixin by following these two tutorials (tutorial-1 and tutorial-2).

This my model so far:

class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField('email address', unique=True, db_index=True)
    username = models.CharField('username', unique=True, db_index=True)
    joined = models.DateField(auto_now_add=True)
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)
    is_staff = models.BooleanField(default=False)

    USERNAME_FIELD = 'email'

    def __unicode__(self):
        return self.email

Now what I am confused about is that in tutorial-1, the author didn't made any custom manager for the custom User model. Instead he use forms for creating user.

class RegistrationForm(forms.ModelForm):
    email = forms.EmailField(label = 'Email')
    password1 = forms.CharField(widget = forms.PasswordInput(), label = "Password")
    password2 = forms.CharField(widget = forms.PasswordInput(), label = 'Retype password')

    class Meta:
        model = User
        fields = ['email', 'username', 'password1', 'password2']

    def clean(self):
        """
        Verify that the values entered into the password fields match
        """
        cleaned_data = super(RegistrationForm, self).clean()
        if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
            if self.cleaned_data['password1'] != self.cleaned_data['password2']:
                raise ValidationError("Password don't match.")
        return self.cleaned_data

    def save(self, commit=True):
        user = super(RegistrationForm, self).save(commit=False)
        user.set_password(self.cleaned_data['password1'])
        if commit:
            user.save()
        return user

But in tutorial-2, its author made a custom manager for the custom User model.

class UserManager(BaseUserManager):

    def create_user(self, email, password, **kwargs):
        user = self.model(
            email=self.normalize_email(email),
            is_active=True,
            **kwargs
        )
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, password, **kwargs):
        user = self.model(
            email=email,
            is_staff=True,
            is_superuser=True,
            is_active=True,
            **kwargs
        )
        user.set_password(password)
        user.save(using=self._db)
        return user

Referencing with Django Docs, there's an example of custom user model, and it uses custom manager. My question is, whether it is ok not to make any other custom manager and if not what is the use of creating a custom manager?
Reply all
Reply to author
Forward
0 new messages