Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Natalie Tan
Natalie Tan
25,519 Points

Django Auth / Custom User Manager: Why is it important to set password=None

https://teamtreehouse.com/library/django-authentication/users-and-authorization/custom-user-manager

Managed to get the code to pass, but why is it important in this step (def create_user(self, email, dob, accepted_tos=False, password=None), to set password's default = None, otherwise it would not pass?

from django.contrib.auth.models import BaseUserManager

class UserManager(BaseUserManager):
    def create_user(self, email, dob, accepted_tos=False, password=None):
        if not accepted_tos:
            raise ValueError("Accept!")
        user = self.model(
            email = self.normalize_email(email),
            dob = dob,
            accepted_tos = accepted_tos
        )
        user.set_password(password)
        user.save()
        return user

[MOD: Added ```python formatting for clarity. -cf]

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Interesting question! What exactly are the minimal requirements regarding the parameter password?

For Task 1, it is not explicitly required to set password=None to pass. I tried several combinations to determine what was required:

Test 1: is the parameter "password" required?

# tried, but failed with
    def create_user(self, email, dob, accepted_tos=False):
    def create_user(self, email, dob, *args, accepted_tos=False):
    def create_user(self, email, dob, passwordish, accepted_tos=False):

# checker must be passing "password" as a keyword argument

Test 2: if required as a keyword argument, does it need a default?

# this passes
    def create_user(self, email, dob, password, accepted_tos=False):
# this also passes
    def create_user(self, email, dob, accepted_tos=False, **kwargs):
# and this passes
    def create_user(self, email, dob, accepted_tos=False, password="Bob"):
        # ...
        # but none seem to care if the parameter was actually used
        # this passes
        user.set_password("Not Bob")
        # so does this
        user.set_unusable_password()  # the equivalent to user.set_password(None)
        user.save()
        return user

So it seems the minimal requirement is that "password" must exist as a parameter, but a default value is not required.

Post back if you wish further details! Good luck!!