1 | class SimpleModel(models.Model):
|
---|
2 | user = models.ForeignKey(User, to_field="username")
|
---|
3 |
|
---|
4 | class ComplexModel(models.Model):
|
---|
5 | relations = models.ManyToManyField(User, through='IntermediateModel')
|
---|
6 |
|
---|
7 | class IntermediateModel(models.Model):
|
---|
8 | user = models.ForeignKey(User, to_field="username")
|
---|
9 | other = models.ForeignKey(M2MToField)
|
---|
10 |
|
---|
11 | class toFieldTests(unittest.TestCase):
|
---|
12 | def setUp(self):
|
---|
13 | self.user = User.objects.create(email="username@sample.com",username="username")
|
---|
14 |
|
---|
15 | def tearDown(self):
|
---|
16 | self.user.delete()
|
---|
17 |
|
---|
18 | class testRelational(self):
|
---|
19 | instance = ComplexModel.objects.create()
|
---|
20 |
|
---|
21 | IntermediateModel.objects.create(other=instance, user=self.user)
|
---|
22 |
|
---|
23 | # Fails
|
---|
24 | self.assertEquals(list(instance.relations.all()), [self.user])
|
---|
25 |
|
---|
26 | class testDeletion(self):
|
---|
27 | instance = ComplexModel.objects.create()
|
---|
28 | IntermediateModel.objects.create(other=instance, user=self.user)
|
---|
29 | instance = SimpleModel.objects.create(user=self.user)
|
---|
30 |
|
---|
31 | for m in (SimpleModel, ComplexModel):
|
---|
32 | m.objects.all().delete()
|
---|
33 |
|
---|
34 | SimpleModel.objects.all().delete()
|
---|
35 | ComplexModel.objects.all().delete()
|
---|
36 |
|
---|
37 | class testSimple(self):
|
---|
38 | instance = SimpleModel.objects.create(user=self.user)
|
---|
39 |
|
---|
40 | self.assertEquals(instance.user, self.user)
|
---|
41 |
|
---|
42 | # Fails
|
---|
43 | self.assertEquals(SimpleModel.objects.get(user=self.user), instance)
|
---|