~ubuntu-branches/ubuntu/quantal/python-django/quantal

« back to all changes in this revision

Viewing changes to tests/regressiontests/null_queries/models.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2009-07-29 11:26:28 UTC
  • mfrom: (1.1.8 upstream) (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090729112628-pg09ino8sz0sj21t
Tags: 1.1-1
* New upstream release.
* Merge from experimental:
  - Ship FastCGI initscript and /etc/default file in python-django's examples
    directory (Closes: #538863)
  - Drop "05_10539-sphinx06-compatibility.diff"; it has been applied
    upstream.
  - Bump Standards-Version to 3.8.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
# A set of models with an inner one pointing to two outer ones.
 
17
class OuterA(models.Model):
 
18
    pass
 
19
 
 
20
class OuterB(models.Model):
 
21
    data = models.CharField(max_length=10)
 
22
 
 
23
class Inner(models.Model):
 
24
    first = models.ForeignKey(OuterA)
 
25
    second = models.ForeignKey(OuterB, null=True)
 
26
 
 
27
__test__ = {'API_TESTS':"""
 
28
# Regression test for the use of None as a query value. None is interpreted as
 
29
# an SQL NULL, but only in __exact queries.
 
30
# Set up some initial polls and choices
 
31
>>> p1 = Poll(question='Why?')
 
32
>>> p1.save()
 
33
>>> c1 = Choice(poll=p1, choice='Because.')
 
34
>>> c1.save()
 
35
>>> c2 = Choice(poll=p1, choice='Why Not?')
 
36
>>> c2.save()
 
37
 
 
38
# Exact query with value None returns nothing ("is NULL" in sql, but every 'id'
 
39
# field has a value).
 
40
>>> Choice.objects.filter(choice__exact=None)
 
41
[]
 
42
 
 
43
Excluding the previous result returns everything.
 
44
>>> Choice.objects.exclude(choice=None).order_by('id')
 
45
[<Choice: Choice: Because. in poll Q: Why? >, <Choice: Choice: Why Not? in poll Q: Why? >]
 
46
 
 
47
# Valid query, but fails because foo isn't a keyword
 
48
>>> Choice.objects.filter(foo__exact=None)
 
49
Traceback (most recent call last):
 
50
...
 
51
FieldError: Cannot resolve keyword 'foo' into field. Choices are: choice, id, poll
 
52
 
 
53
# Can't use None on anything other than __exact
 
54
>>> Choice.objects.filter(id__gt=None)
 
55
Traceback (most recent call last):
 
56
...
 
57
ValueError: Cannot use None as a query value
 
58
 
 
59
# Can't use None on anything other than __exact
 
60
>>> Choice.objects.filter(foo__gt=None)
 
61
Traceback (most recent call last):
 
62
...
 
63
ValueError: Cannot use None as a query value
 
64
 
 
65
# Related managers use __exact=None implicitly if the object hasn't been saved.
 
66
>>> p2 = Poll(question="How?")
 
67
>>> p2.choice_set.all()
 
68
[]
 
69
 
 
70
# Querying across reverse relations and then another relation should insert
 
71
# outer joins correctly so as not to exclude results.
 
72
>>> obj = OuterA.objects.create()
 
73
>>> OuterA.objects.filter(inner__second=None)
 
74
[<OuterA: OuterA object>]
 
75
>>> OuterA.objects.filter(inner__second__data=None)
 
76
[<OuterA: OuterA object>]
 
77
>>> _ = Inner.objects.create(first=obj)
 
78
>>> Inner.objects.filter(first__inner__second=None)
 
79
[<Inner: Inner object>]
 
80
 
 
81
 
 
82
"""}