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 testSimple(self):
|
---|
27 | instance = SimpleModel.objects.create(user=self.user)
|
---|
28 |
|
---|
29 | self.assertEquals(instance.user, self.user)
|
---|
30 |
|
---|
31 | # Fails
|
---|
32 | self.assertEquals(SimpleModel.objects.get(user=self.user), instance)
|
---|