Create User method for Custom Manager Django

70 views
Skip to first unread message

Max Nathaniel Ho

unread,
Feb 3, 2015, 1:19:47 AM2/3/15
to django...@googlegroups.com

Hi all,

I am following this tutorial (http://musings.tinbrain.net/blog/2014/sep/21/registration-django-easy-way/) to create a user registration model in Django.

I understand that the class UserManager is overwriting the default User model. However, I do not understand this particular part.

The official Django Documentation doesn't explain what this means - It merely shows the full code.

https://docs.djangoproject.com/en/1.7/topics/auth/customizing/

Need some clarification as to what's going on here. What is self.model in this example and what does it do?

    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
This is the entire code. 

        
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
        

Daniel Roseman

unread,
Feb 3, 2015, 9:12:08 AM2/3/15
to django...@googlegroups.com

On Tuesday, 3 February 2015 06:19:47 UTC, Max Nathaniel Ho wrote:

Hi all,

I am following this tutorial (http://musings.tinbrain.net/blog/2014/sep/21/registration-django-easy-way/) to create a user registration model in Django.

I understand that the class UserManager is overwriting the default User model. However, I do not understand this particular part.

The official Django Documentation doesn't explain what this means - It merely shows the full code.

https://docs.djangoproject.com/en/1.7/topics/auth/customizing/

Need some clarification as to what's going on here. What is self.model in this example and what does it do?

    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


It is not doing any such thing as "overwriting the default User model". That is a Manager: your first port of call should have been the documentation for model managers, which is here:

If there's anything you don't understand after having read that, please come back and ask a more specific question.
--
DR.

Max Nathaniel Ho

unread,
Feb 4, 2015, 4:20:36 AM2/4/15
to django...@googlegroups.com
Read it. 

Understand it now. Thanks!
Reply all
Reply to author
Forward
0 new messages