![Show Menu](styles/mobile-menu.png)
![Page Background](./../common/page-substrates/page0321.png)
That gets us past the first assertion, and onto
self.assertEqual(found_user, desired_user)
AssertionError: <User: otheruser> != <User: >
And so we call
get
with the email as an argument:
accounts/authentication.py (ch16l034).
def
get_user
(
self
,
):
return
User
.
objects
.
get
(
=
)
That gets us to passing tests:
Now our test for the None case fails:
ERROR: test_returns_none_if_no_user_with_that_email
[...]
django.contrib.auth.models.DoesNotExist: User matching query does not exist.
Which prompts us to finish the method like this:
accounts/authentication.py (ch16l035).
def
get_user
(
self
,
):
try
:
return
User
.
objects
.
get
(
=
)
except
User
.
DoesNotExist
:
return
None
#
You could just use
pass
here, and the function would return
None
by default.
However, because we specifically need the function to return
None
, explicit is
better than implicit here.
That gets us to passing tests:
OK
And we have a working authentication backend!
$
python3 manage.py test accounts
[...]
Ran 11 tests in 0.020s
OK
Now we can define our custom user model.
A Minimal Custom User Model
Django’s built-in user model makes all sorts of assumptions about what information
you want to track about users, from explicitly recording first name and last name, to
forcing you to use a username. I’m a great believer in not storing information about
users unless you absolutely must, so a user model that records an email address and
nothing else sounds good to me!
A Minimal Custom User Model
|
293