1 from django.db import models |
|
2 |
|
3 class Poll(models.Model): |
|
4 question = models.CharField(maxlength=200) |
|
5 |
|
6 def __str__(self): |
|
7 return "Q: %s " % self.question |
|
8 |
|
9 class Choice(models.Model): |
|
10 poll = models.ForeignKey(Poll) |
|
11 choice = models.CharField(maxlength=200) |
|
12 |
|
13 def __str__(self): |
|
14 return "Choice: %s in poll %s" % (self.choice, self.poll) |
|
15 |
|
16 __test__ = {'API_TESTS':""" |
|
17 # Regression test for the use of None as a query value. None is interpreted as |
|
18 # an SQL NULL, but only in __exact queries. |
|
19 # Set up some initial polls and choices |
|
20 >>> p1 = Poll(question='Why?') |
|
21 >>> p1.save() |
|
22 >>> c1 = Choice(poll=p1, choice='Because.') |
|
23 >>> c1.save() |
|
24 >>> c2 = Choice(poll=p1, choice='Why Not?') |
|
25 >>> c2.save() |
|
26 |
|
27 # Exact query with value None returns nothing (=NULL in sql) |
|
28 >>> Choice.objects.filter(id__exact=None) |
|
29 [] |
|
30 |
|
31 # Valid query, but fails because foo isn't a keyword |
|
32 >>> Choice.objects.filter(foo__exact=None) |
|
33 Traceback (most recent call last): |
|
34 ... |
|
35 TypeError: Cannot resolve keyword 'foo' into field |
|
36 |
|
37 # Can't use None on anything other than __exact |
|
38 >>> Choice.objects.filter(id__gt=None) |
|
39 Traceback (most recent call last): |
|
40 ... |
|
41 ValueError: Cannot use None as a query value |
|
42 |
|
43 # Can't use None on anything other than __exact |
|
44 >>> Choice.objects.filter(foo__gt=None) |
|
45 Traceback (most recent call last): |
|
46 ... |
|
47 ValueError: Cannot use None as a query value |
|
48 |
|
49 # Related managers use __exact=None implicitly if the object hasn't been saved. |
|
50 >>> p2 = Poll(question="How?") |
|
51 >>> p2.choice_set.all() |
|
52 [] |
|
53 |
|
54 """} |
|