1 from django
.db
import models
3 class Poll(models
.Model
):
4 question
= models
.CharField(max_length
=200)
7 return u
"Q: %s " % self
.question
9 class Choice(models
.Model
):
10 poll
= models
.ForeignKey(Poll
)
11 choice
= models
.CharField(max_length
=200)
13 def __unicode__(self
):
14 return u
"Choice: %s in poll %s" % (self
.choice
, self
.poll
)
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?')
22 >>> c1 = Choice(poll=p1, choice='Because.')
24 >>> c2 = Choice(poll=p1, choice='Why Not?')
27 # Exact query with value None returns nothing ("is NULL" in sql, but every 'id'
29 >>> Choice.objects.filter(choice__exact=None)
32 Excluding the previous result returns everything.
33 >>> Choice.objects.exclude(choice=None).order_by('id')
34 [<Choice: Choice: Because. in poll Q: Why? >, <Choice: Choice: Why Not? in poll Q: Why? >]
36 # Valid query, but fails because foo isn't a keyword
37 >>> Choice.objects.filter(foo__exact=None)
38 Traceback (most recent call last):
40 FieldError: Cannot resolve keyword 'foo' into field. Choices are: choice, id, poll
42 # Can't use None on anything other than __exact
43 >>> Choice.objects.filter(id__gt=None)
44 Traceback (most recent call last):
46 ValueError: Cannot use None as a query value
48 # Can't use None on anything other than __exact
49 >>> Choice.objects.filter(foo__gt=None)
50 Traceback (most recent call last):
52 ValueError: Cannot use None as a query value
54 # Related managers use __exact=None implicitly if the object hasn't been saved.
55 >>> p2 = Poll(question="How?")
56 >>> p2.choice_set.all()