File "/workspace/superlists/lists/views.py", line 14, in new_list
Item.objects.create(text=request.POST['item_text'])
It’s when we try and create an item without a parent list. So we make a similar change
in the view:
lists/views.py.
from
lists.models
import
Item
,
List
[
...
]
def
new_list
(
request
):
list_
=
List
.
objects
.
create
()
Item
.
objects
.
create
(
text
=
request
.
POST
[
'item_text'
],
list
=
list_
)
return
redirect
(
'/lists/the-only-list-in-the-world/'
)
And that gets our tests passing again:
OK
Are you cringing internally at this point?
Arg! This feels so wrong, we create a new list
for every single new item submission, andwe’re still just displaying all items as if they belong
to the same list!
I know, I feel the same. The step-by-step approach, in which you go
from working code to working code, is counterintuitive. I always feel like just diving in
and trying to fix everything all in one go, instead of going from one weird half-finished
state to another. But remember the Testing Goat! When you’re up a mountain, you want
to think very carefully about where you put each foot, and take one step at a time,
checking at each stage that the place you’ve put it hasn’t caused you to fall off a cliff.
So just to reassure ourselves that things have worked, we rerun the FT. Sure enough, it
gets all the way through to where we were before. We haven’t broken anything, and we’ve
made a change to the database. That’s something to be pleased with! Let’s commit:
$
git status
# 3 changed files, plus 2 migrations
$
git add lists
$
git diff --staged
$
git commit
And we can cross out another item on the to-do list:
Adjusting Our Models
|
101