Background Image
Table of Contents Table of Contents
Previous Page  321 / 478 Next Page
Information
Show Menu
Previous Page 321 / 478 Next Page
Page Background

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

,

email

):

return

User

.

objects

.

get

(

email

=

email

)

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

,

email

):

try

:

return

User

.

objects

.

get

(

email

=

email

)

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