1 | """
|
---|
2 | Temp
|
---|
3 | """
|
---|
4 |
|
---|
5 | from django.db import models
|
---|
6 |
|
---|
7 | class User(models.Model):
|
---|
8 | username = models.CharField(maxlength=20)
|
---|
9 |
|
---|
10 | class Issue(models.Model):
|
---|
11 | num = models.IntegerField()
|
---|
12 | cc = models.ManyToManyField(User, blank=True, related_name='test_issue_cc')
|
---|
13 | client = models.ForeignKey(User, related_name='test_issue_client')
|
---|
14 | def __repr__(self):
|
---|
15 | return "<Issue %d>" % (self.num,)
|
---|
16 |
|
---|
17 | class Meta:
|
---|
18 | ordering = ('num',)
|
---|
19 |
|
---|
20 |
|
---|
21 | API_TESTS = """
|
---|
22 | >>> Issue.objects.all()
|
---|
23 | []
|
---|
24 | >>> r = User(username='russell')
|
---|
25 | >>> r.save()
|
---|
26 | >>> g = User(username='gustav')
|
---|
27 | >>> g.save()
|
---|
28 | >>> i = Issue(num=1)
|
---|
29 | >>> i.client = r
|
---|
30 | >>> i.validate()
|
---|
31 | {}
|
---|
32 | >>> i.save()
|
---|
33 | >>> i2 = Issue(num=2)
|
---|
34 | >>> i2.client = r
|
---|
35 | >>> i2.validate()
|
---|
36 | {}
|
---|
37 | >>> i2.save()
|
---|
38 | >>> i2.cc.add(r)
|
---|
39 | >>> i3 = Issue(num=3)
|
---|
40 | >>> i3.client = g
|
---|
41 | >>> i3.validate()
|
---|
42 | {}
|
---|
43 | >>> i3.save()
|
---|
44 | >>> i3.cc.add(r)
|
---|
45 | >>> from django.db.models.query import Q
|
---|
46 | >>> Issue.objects.filter(client=r.id)
|
---|
47 | [<Issue 1>, <Issue 2>]
|
---|
48 | >>> Issue.objects.filter(client=g.id)
|
---|
49 | [<Issue 3>]
|
---|
50 | >>> Issue.objects.filter(cc__id__exact=g.id)
|
---|
51 | []
|
---|
52 | >>> Issue.objects.filter(cc__id__exact=r.id)
|
---|
53 | [<Issue 2>, <Issue 3>]
|
---|
54 | >>> Issue.objects.filter(Q(cc__id__exact=r.id) | Q(client=r.id))
|
---|
55 | [<Issue 1>, <Issue 2>, <Issue 3>]
|
---|
56 | >>> Issue.objects.filter(cc__id__exact=r.id) | Issue.objects.filter(client=r.id)
|
---|
57 | [<Issue 1>, <Issue 2>, <Issue 3>]
|
---|
58 | >>> Issue.objects.filter(Q(client=r.id) | Q(cc__id__exact=r.id))
|
---|
59 | [<Issue 1>, <Issue 2>, <Issue 3>]
|
---|
60 | """
|
---|