Merged the queryset-refactor branch into trunk.
[fdr-django.git] / tests / regressiontests / null_queries / models.py
blobdf04f14eba8d465b44f93c1f1b09d6b7340d38e7
1 from django.db import models
3 class Poll(models.Model):
4 question = models.CharField(max_length=200)
6 def __unicode__(self):
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?')
21 >>> p1.save()
22 >>> c1 = Choice(poll=p1, choice='Because.')
23 >>> c1.save()
24 >>> c2 = Choice(poll=p1, choice='Why Not?')
25 >>> c2.save()
27 # Exact query with value None returns nothing ("is NULL" in sql, but every 'id'
28 # field has a value).
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):
39 ...
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):
45 ...
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):
51 ...
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()
59 """}