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 Django Authentication Users and Authorization Custom User Manager

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

Bummer! User password...

CodeChallenge not passing---

Extend BaseUserManager to make a new UserManager class. (done)

Add a method named create_user that creates a user. The method should accept email, dob (date of birth), accepted_tos, and password arguments. (done)

If accepted_tos isn't True, raise a ValueError. The text is up to you. (done)

After that, create a user (use self.model) with all of the accepted arguments, set the user's password, then save and return the user. (done)

After following instructions carefully, it won't pass. Yes, I tried a variant where I put the password in the model... that is not how it is supposed to be done, but I tried it for sake of getting code to pass (it did not).

The diagnostic is not helpful, and I have tried about five different variations to try to get step 1 to pass. This is the second very frustrating challenge of this course that doesn't seem to give good diagnostics or hints.

accounts/models.py
from django.contrib.auth.models import (
    AbstractBaseUser,
    BaseUserManager,
    PermissionsMixin
)

from django.db import models

class UserManager(BaseUserManager):
    def create_user(email, dob, accepted_tos=False, password=None):
        if accepted_tos != True:
            raise ValueError("Users must first accept the terms of service")
        user = self.model(email=self.normalize_email(email), dob=dob, accepted_tos=accepted_tos)
        user.set_password(password)
        user.save()
        return user
Jeff Muday
Jeff Muday
Treehouse Moderator 28,720 Points

Ok, I solved my problem.

Hints:

  1. accepted_tos needs to be set to True when creating the user model.
  2. the password needs to go in as a parameter in the user model when it is created.
user = self.model(email=email, dob=dob, accepted_tos=True, password=password)
James J. McCombie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
James J. McCombie
Python Web Development Techdegree Graduate 21,199 Points

Can you paste the code you used to pass this Jeff?

I am banging my head against a wall over this and 'Bummer! Try Again!' is really not helpful. These code challenges are getting more and more frustrating, they seem to deviate evermore so from what is taught in the lessons and reject solutions that work perfectly well in practice due to very obtuse reasons.

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

Note... must include 'self' for it to be syntactically correct. The tests that were written cannot detect this.

def create_user(self, email, dob, accepted_tos=False, password=None):
        if accepted_tos != True:
            raise ValueError("Users must first accept the terms of service")
        u = self.model(email=email, dob=dob, accepted_tos=True, password=password)
        u.set_password(password)
        u.save()
        return u