Selaa lähdekoodia

Adjust model so that items are associated with different lists

david 5 vuotta sitten
vanhempi
commit
a2f30b540a
4 muutettua tiedostoa jossa 15 lisäystä ja 13 poistoa
  1. 1 1
      SCRATCHPAD.txt
  2. 4 3
      lists/models.py
  3. 7 7
      lists/tests.py
  4. 3 2
      lists/views.py

+ 1 - 1
SCRATCHPAD.txt

@@ -5,7 +5,7 @@
 + Remove time.sleeps
 + Support more than 1 list!
 
-+ Adjust model so that items are associated with different lists
+- Adjust model so that items are associated with different lists
 + Add unique URLs for each list
 - Add a URL for creating a new list via POST
 + Add URLs for adding a new item to an existing list via POST

+ 4 - 3
lists/models.py

@@ -1,7 +1,8 @@
 from django.db import models
 
+class List(models.Model):
+    pass
+
 class Item(models.Model):
     text = models.TextField(default='')
-
-class List(models.Model):
-    pass
+    listi = models.ForeignKey(List, default=None)

+ 7 - 7
lists/tests.py

@@ -26,7 +26,6 @@ class HomePageTest(TestCase):
 
     def test_can_save_POST_request(self):
         response = self.client.post('/', data={'item_text': 'A new list item'})
-
         self.assertEqual(Item.objects.count(), 1)
         new_item = Item.objects.first()
         self.assertEqual(new_item.text, 'A new list item')
@@ -43,8 +42,9 @@ class ListViewTest(TestCase):
         self.assertTemplateUsed(response, 'list.html')
 
     def test_displays_all_items(self):
-        Item.objects.create(text='itemey 1')
-        Item.objects.create(text='itemey 2')
+        list_ = List.objects.create()
+        Item.objects.create(text='itemey 1', listi=list_)
+        Item.objects.create(text='itemey 2', listi=list_)
 
         response = self.client.get('/lists/the-only-list/')
 
@@ -58,12 +58,12 @@ class ListAndItemModelsTest(TestCase):
         list_.save()
         item1 = Item()
         item1.text = 'The first (ever) list item'
-        item1.list = list_
+        item1.listi = list_
         item1.save()
         
         item2 = Item()
         item2.text = 'Item the 2nd'
-        item2.list = list_
+        item2.listi = list_
         item2.save()
 
         savedL = List.objects.first()
@@ -75,9 +75,9 @@ class ListAndItemModelsTest(TestCase):
         first_item = savedI[0]
         second_item = savedI[1]
         self.assertEqual(first_item.text, item1.text)
-        self.assertEqual(first_item.list, list_)
+        self.assertEqual(first_item.listi, list_)
         self.assertEqual(second_item.text, item2.text)
-        self.assertEqual(second_item.list, list_)
+        self.assertEqual(second_item.listi, list_)
 
 class NewListTest(TestCase):
 

+ 3 - 2
lists/views.py

@@ -1,6 +1,6 @@
 from django.shortcuts import render, redirect
 #from django.http import HttpResponse
-from lists.models import Item
+from lists.models import Item, List
 
 def home_page(request):
     return render(request, 'home.html')
@@ -10,5 +10,6 @@ def view_list(request):
     return render(request, 'list.html', {'items': items})
 
 def new_list(request):
-    Item.objects.create(text=request.POST['item_text'])
+    list_ = List.objects.create()
+    Item.objects.create(text=request.POST['item_text'], listi=list_)
     return redirect('/lists/the-only-list/')