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

That will give us all sorts of integrity failures, until we do a migration:

django.db.utils.OperationalError: no such column: lists_list.owner_id

FAILED (errors=28)

Building the migration will get us down to three failures:

ERROR: test_create_new_optionally_saves_owner

TypeError: create_new() got an unexpected keyword argument 'owner'

[...]

ValueError: Cannot assign "<SimpleLazyObject:

<django.contrib.auth.models.AnonymousUser object at 0x7f5b2380b4e0>>":

"List.owner" must be a "User" instance.

ValueError: Cannot assign "<SimpleLazyObject:

<django.contrib.auth.models.AnonymousUser object at 0x7f5b237a12e8>>":

"List.owner" must be a "User" instance.

Let’s deal with the first one, which is for our

create_new

method:

lists/models.py (ch19l030-3).

@staticmethod

def

create_new

(

first_item_text

,

owner

=

None

):

list_

=

List

.

objects

.

create

(

owner

=

owner

)

Item

.

objects

.

create

(

text

=

first_item_text

,

list

=

list_

)

Back to Views

Two of our old integrated tests for the views layer are failing. What’s happening?

ValueError: Cannot assign "<SimpleLazyObject:

<django.contrib.auth.models.AnonymousUser object at 0x7fbad1cb6c10>>":

"List.owner" must be a "User" instance.

Ah, the old view isn’t discerning enough about what it does with list owners yet:

lists/views.py.

if

form

.

is_valid

():

list_

=

List

()

list_

.

owner

=

request

.

user

list_

.

save

()

This is the point at which we realise that our old code wasn’t fit for purpose. Let’s fix it

to get all our tests passing:

lists/views.py (ch19l031).

def

new_list

(

request

):

form

=

ItemForm

(

data

=

request

.

POST

)

if

form

.

is_valid

():

list_

=

List

()

if

request

.

user

.

is_authenticated

():

list_

.

owner

=

request

.

user

list_

.

save

()

form

.

save

(

for_list

=

list_

)

Finally, Moving Down to the Models Layer

|

353