Skip to content

Commit 7080c48

Browse files
committed
first stab at updating 15
1 parent 4214205 commit 7080c48

File tree

3 files changed

+29
-33
lines changed

3 files changed

+29
-33
lines changed

chapter_15_advanced_forms.asciidoc

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ If we want to get it deliberately wrong, we can do this:
135135
[source,python]
136136
----
137137
class Item(models.Model):
138-
text = models.TextField(default='', unique=True)
139-
list = models.ForeignKey(List, default=None)
138+
text = models.TextField(default="", unique=True)
139+
list = models.ForeignKey(List, default=None, on_delete=models.CASCADE)
140140
----
141141
====
142142

@@ -412,6 +412,8 @@ works; we get a fully passing test suite:
412412
OK
413413
----
414414

415+
// TODO: in theory we should do a migration now.
416+
415417
[[rewrite-model-test]]
416418
Rewriting the Old Model Test
417419
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -429,18 +431,17 @@ Delete `test_saving_and_retrieving_items` and replace with this:
429431
[source,python]
430432
----
431433
class ListAndItemModelsTest(TestCase):
432-
433434
def test_default_text(self):
434435
item = Item()
435-
self.assertEqual(item.text, '')
436+
self.assertEqual(item.text, "")
436437
437-
438438
def test_item_is_related_to_list(self):
439-
list_ = List.objects.create()
439+
mylist = List.objects.create()
440440
item = Item()
441-
item.list = list_
441+
item.list = mylist
442442
item.save()
443-
self.assertIn(item, list_.item_set.all())
443+
self.assertIn(item, mylist.item_set.all())
444+
444445
445446
[...]
446447
----
@@ -461,14 +462,11 @@ for `List` (there's only one of the latter, `test_get_absolute_url`):
461462
[source,python]
462463
----
463464
class ItemModelTest(TestCase):
464-
465465
def test_default_text(self):
466466
[...]
467467
468468
469-
470469
class ListModelTest(TestCase):
471-
472470
def test_get_absolute_url(self):
473471
[...]
474472
----
@@ -505,7 +503,7 @@ it as an application-layer constraint:
505503
----
506504
$ pass:quotes[*python manage.py makemigrations*]
507505
Migrations for 'lists':
508-
lists/migrations/0005_auto_20140414_2038.py
506+
src/lists/migrations/0005_alter_item_options_alter_item_unique_together.py
509507
- Change Meta options on item
510508
- Alter unique_together for item (1 constraint(s))
511509
----
@@ -563,7 +561,6 @@ now it's time to commit our model-layer changes:
563561
----
564562
$ pass:[<strong>git status</strong>] # should show changes to tests + models and new migration
565563
# let's give our new migration a better name
566-
$ pass:[<strong>mv lists/migrations/0005_auto* lists/migrations/0005_list_item_unique_together.py</strong>]
567564
$ pass:[<strong>git add lists</strong>]
568565
$ pass:[<strong>git diff --staged</strong>]
569566
$ pass:[<strong>git commit -am "Implement duplicate item validation at model layer"</strong>]
@@ -659,32 +656,31 @@ We duplicate our tests for the previous form, tweaking them slightly:
659656
[source,python]
660657
----
661658
from lists.forms import (
662-
DUPLICATE_ITEM_ERROR, EMPTY_ITEM_ERROR,
663-
ExistingListItemForm, ItemForm
659+
DUPLICATE_ITEM_ERROR,
660+
EMPTY_ITEM_ERROR,
661+
ExistingListItemForm,
662+
ItemForm,
664663
)
665664
[...]
666665
667666
class ExistingListItemFormTest(TestCase):
668-
669667
def test_form_renders_item_text_input(self):
670668
list_ = List.objects.create()
671669
form = ExistingListItemForm(for_list=list_)
672670
self.assertIn('placeholder="Enter a to-do item"', form.as_p())
673671
674-
675672
def test_form_validation_for_blank_items(self):
676673
list_ = List.objects.create()
677-
form = ExistingListItemForm(for_list=list_, data={'text': ''})
674+
form = ExistingListItemForm(for_list=list_, data={"text": ""})
678675
self.assertFalse(form.is_valid())
679-
self.assertEqual(form.errors['text'], [EMPTY_ITEM_ERROR])
680-
676+
self.assertEqual(form.errors["text"], [EMPTY_ITEM_ERROR])
681677
682678
def test_form_validation_for_duplicate_items(self):
683679
list_ = List.objects.create()
684-
Item.objects.create(list=list_, text='no twins!')
685-
form = ExistingListItemForm(for_list=list_, data={'text': 'no twins!'})
680+
Item.objects.create(list=list_, text="no twins!")
681+
form = ExistingListItemForm(for_list=list_, data={"text": "no twins!"})
686682
self.assertFalse(form.is_valid())
687-
self.assertEqual(form.errors['text'], [DUPLICATE_ITEM_ERROR])
683+
self.assertEqual(form.errors["text"], [DUPLICATE_ITEM_ERROR])
688684
----
689685
====
690686

@@ -826,16 +822,16 @@ class ListViewTest(TestCase):
826822
[...]
827823
828824
def test_displays_item_form(self):
829-
list_ = List.objects.create()
830-
response = self.client.get(f'/lists/{list_.id}/')
831-
self.assertIsInstance(response.context['form'], ExistingListItemForm)
825+
mylist = List.objects.create()
826+
response = self.client.get(f"/lists/{mylist.id}/")
827+
self.assertIsInstance(response.context["form"], ExistingListItemForm)
832828
self.assertContains(response, 'name="text"')
833829
834830
[...]
835831
836832
def test_for_invalid_input_passes_form_to_template(self):
837833
response = self.post_invalid_input()
838-
self.assertIsInstance(response.context['form'], ExistingListItemForm)
834+
self.assertIsInstance(response.context["form"], ExistingListItemForm)
839835
----
840836
====
841837

@@ -856,10 +852,10 @@ So we can adjust the view:
856852
from lists.forms import ExistingListItemForm, ItemForm
857853
[...]
858854
def view_list(request, list_id):
859-
list_ = List.objects.get(id=list_id)
860-
form = ExistingListItemForm(for_list=list_)
861-
if request.method == 'POST':
862-
form = ExistingListItemForm(for_list=list_, data=request.POST)
855+
our_list = List.objects.get(id=list_id)
856+
form = ExistingListItemForm(for_list=our_list)
857+
if request.method == "POST":
858+
form = ExistingListItemForm(for_list=our_list, data=request.POST)
863859
if form.is_valid():
864860
form.save()
865861
[...]

source/blackify-chap.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ git reset --hard $REPO/$PREV
1919
ruff format .
2020
git commit -am"initial black commit" --allow-empty
2121

22-
git rev-list $STARTCOMMIT^..$ENDCOMMIT| tac | xargs -n1 sh -c 'git co $0 -- . && ruff format . && git add . && git stwdiff && git commit -am "$(git show -s --format=%B $0)"'
22+
git rev-list $STARTCOMMIT^..$ENDCOMMIT| tail -r | xargs -n1 sh -c 'git co $0 -- . && ruff format . && git add . && git stwdiff && git commit -am "$(git show -s --format=%B $0)"'
2323

2424
git diff -w $REPO/$CHAP
2525

Submodule superlists updated 115 files

0 commit comments

Comments
 (0)