class Boo models Model title models CharField max_length 20 slug model

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Boo(models.Model):
title = models.CharField(max_length=20)
slug = models.SlugField()
class Meta:
unique_together = ('title', 'slug')
class Foo(models.Model):
boo = models.ForeignKey(Boo)
################################################################
class TestQueries:
def test_saveobject(self):
call_command('syncdb', interactive=False, verbosity=0)
Boo.objects.all().delete()
Foo.objects.all().delete()
assert_equal(Boo.objects.all().count(), 0)
b = Boo()
b.title = "First Title"
b.slug = "first_title"
b.save()
b2 = Boo()
b2.title = "Second Title"
b2.slug = "second_title"
b2.save()
assert_equal(Boo.objects.all().count(), 2)
assert_equal(Boo.objects.filter(slug="first_title").count(),1)
f = Foo(boo=b)
f.save()
assert_equal(b.foo_set.count(), 1)
assert_equal(b2.foo_set.count(), 0)
f2 = Foo(boo=b2)
f2.save()
new_f = Foo.objects.filter(boo=b)[0]
assert new_f.boo.title == "First Title"