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

accounts/models.py.

class

User

(

models

.

Model

):

email

=

models

.

EmailField

()

REQUIRED_FIELDS

=

()

USERNAME_FIELD

=

'email'

The next error is a database error:

django.db.utils.OperationalError: no such table: accounts_user

That prompts us, as usual, to do a migration:

$

python3 manage.py makemigrations

System check identified some issues:

WARNINGS:

accounts.User: (auth.W004) 'User.email' is named as the 'USERNAME_FIELD', but

it is not unique.

HINT: Ensure that your authentication backend(s) can handle non-unique

usernames.

Migrations for 'accounts':

0001_initial.py:

- Create model User

Let’s hold that thought, and see if we can get the tests passing again.

A Slight Disappointment

Meanwhile, we have a couple of weird unexpected failures:

$

python3 manage.py test accounts

[...]

ERROR: test_gets_logged_in_session_if_authenticate_returns_a_user

[...]

ERROR: test_returns_OK_when_user_found

[...]

user.save(update_fields=['last_login'])

[...]

ValueError: The following fields do not exist in this model or are m2m fields:

last_login

It looks like Django is going to insist on us having a

last_login

field on our user model

too. Oh well. My pristine, single-field user model is despoiled. I still love it though.

accounts/models.py.

from

django.db

import

models

from

django.utils

import

timezone

class

User

(

models

.

Model

):

email

=

models

.

EmailField

()

last_login

=

models

.

DateTimeField

(

default

=

timezone

.

now

)

REQUIRED_FIELDS

=

()

USERNAME_FIELD

=

'email'

We get another database error, so let’s clear down the migration and re-create it:

A Minimal Custom User Model

|

295