root/django/trunk/tests/regressiontests/null_queries/models.py
| Revision 7477, 1.7 kB (checked in by mtredinnick, 4 months ago) | |
|---|---|
| |
| 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.
