Django

Code

root/django/trunk/tests/regressiontests/null_queries/models.py

Revision 7477, 1.7 kB (checked in by mtredinnick, 4 months ago)

Merged the queryset-refactor branch into trunk.

This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.

Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658

  • Property svn:eol-style set to native
Line 
1 from django.db import models
2
3 class Poll(models.Model):
4     question = models.CharField(max_length=200)
5
6     def __unicode__(self):
7         return u"Q: %s " % self.question
8
9 class Choice(models.Model):
10     poll = models.ForeignKey(Poll)
11     choice = models.CharField(max_length=200)
12
13     def __unicode__(self):
14         return u"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 ("is NULL" in sql, but every 'id'
28 # field has a value).
29 >>> Choice.objects.filter(choice__exact=None)
30 []
31
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? >]
35
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
41
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
47
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
53
54 # Related managers use __exact=None implicitly if the object hasn't been saved.
55 >>> p2 = Poll(question="How?")
56 >>> p2.choice_set.all()
57 []
58
59 """}
Note: See TracBrowser for help on using the browser.