@@ -736,6 +736,7 @@ Let's write a test for that:
736
736
[source,python]
737
737
----
738
738
from django.contrib.auth import get_user_model
739
+ [...]
739
740
740
741
User = get_user_model()
741
742
[...]
@@ -757,7 +758,7 @@ And that gives us a new unit test failure:
757
758
----
758
759
mylist = List.objects.create(owner=user)
759
760
[...]
760
- TypeError: 'owner' is an invalid keyword argument for this function
761
+ TypeError: List() got unexpected keyword arguments: 'owner'
761
762
----
762
763
763
764
The naive implementation would be this:
@@ -790,7 +791,7 @@ that too:
790
791
The correct implementation is this:
791
792
792
793
[role="sourcecode"]
793
- .src/lists/models.py (ch22l021)
794
+ .src/lists/models.py (ch22l021)
794
795
====
795
796
[source,python]
796
797
----
@@ -806,13 +807,13 @@ class List(models.Model):
806
807
return reverse("view_list", args=[self.id])
807
808
----
808
809
====
809
- //21
810
810
811
811
Now running the tests gives the usual database error:
812
812
813
813
----
814
- return Database.Cursor.execute(self, query, params)
815
- django.db.utils.OperationalError: no such column: lists_list.owner_id
814
+ return super().execute(query, params)
815
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
816
+ django.db.utils.OperationalError: table lists_list has no column named owner_id
816
817
----
817
818
818
819
@@ -822,36 +823,39 @@ Because we need to make some migrations:
822
823
----
823
824
$ pass:quotes[*python src/manage.py makemigrations*]
824
825
Migrations for 'lists':
825
- lists/migrations/0006_list_owner.py
826
+ src/ lists/migrations/0006_list_owner.py
826
827
- Add field owner to list
827
828
----
828
829
//22
829
830
830
831
We're almost there; a couple more failures:
831
832
832
833
----
833
- ERROR: test_redirects_after_POST (lists.tests.test_views.NewListTest)
834
+ ERROR: test_can_save_a_POST_request
834
835
[...]
835
836
ValueError: Cannot assign "<SimpleLazyObject:
836
- <django.contrib.auth.models.AnonymousUser object at 0x7f364795ef90 >>":
837
- "List.owner" must be a "User" instance.
838
- ERROR: test_can_save_a_POST_request (lists.tests.test_views.NewListTest)
837
+ <django.contrib.auth.models.AnonymousUser object at 0x1069852e >>": "List.owner" must
838
+ be a "User" instance.
839
+ [...]
839
840
841
+ ERROR: test_redirects_after_POST
840
842
[...]
841
843
ValueError: Cannot assign "<SimpleLazyObject:
842
- <django.contrib.auth.models.AnonymousUser object at 0x7f364795ef90 >>":
843
- "List.owner" must be a "User" instance.
844
+ <django.contrib.auth.models.AnonymousUser object at 0x106a1b440 >>": "List.owner" must
845
+ be a "User" instance.
844
846
----
845
847
846
848
847
849
848
- We're moving back up to the views layer now, just doing a little
849
- tidying up. Notice that these are in the old test for the `new_list` view, when
850
- we haven't got a logged-in user. We should only save the list owner when the
851
- user is actually logged in. The `.is_authenticated` attribute we defined in
852
- <<chapter_18_spiking_custom_auth>> comes in useful now (when they're not logged in,
853
- Django represents users using a class called `AnonymousUser`, whose
854
- `.is_authenticated` is always `False`):
850
+ We're moving back up to the views layer now, just doing a little tidying up.
851
+ Notice that these are in the old test for the `new_list` view,
852
+ when we haven't got a logged-in user.
853
+ We should only save the list owner when the user is actually logged in.
854
+ The `.is_authenticated` attribute we defined in <<chapter_18_spiking_custom_auth>>
855
+ comes in useful now
856
+ (when they're not logged in,
857
+ Django represents users using a class called `AnonymousUser`,
858
+ whose `.is_authenticated` is always `False`):
855
859
856
860
857
861
[role="sourcecode"]
@@ -876,9 +880,8 @@ And that gets us passing!
876
880
----
877
881
$ pass:quotes[*python src/manage.py test lists*]
878
882
[...]
879
- .......................................
880
- ---------------------------------------------------------------------
881
- Ran 39 tests in 0.237s
883
+
884
+ Ran 37 tests in 0.237s
882
885
883
886
OK
884
887
----
@@ -887,14 +890,13 @@ This is a good time for a commit:
887
890
888
891
[subs="specialcharacters,quotes"]
889
892
----
890
- $ *git add lists*
893
+ $ *git add src/ lists*
891
894
$ *git commit -m "lists can have owners, which are saved on creation."*
892
895
----
893
896
894
897
895
898
896
- Final Step: Feeding Through the .name API from the Template
897
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
899
+ === Final Step: Feeding Through the .name API from the Template
898
900
899
901
The last thing our outside-in design wanted came from the templates,
900
902
which wanted to be able to access a list "name" based on the text of
@@ -907,9 +909,9 @@ its first item:
907
909
----
908
910
def test_list_name_is_first_item_text(self):
909
911
list_ = List.objects.create()
910
- Item.objects.create(list=list_, text=' first item' )
911
- Item.objects.create(list=list_, text=' second item' )
912
- self.assertEqual(list_.name, ' first item' )
912
+ Item.objects.create(list=list_, text=" first item" )
913
+ Item.objects.create(list=list_, text=" second item" )
914
+ self.assertEqual(list_.name, " first item" )
913
915
----
914
916
====
915
917
0 commit comments