~ubuntu-branches/ubuntu/jaunty/python-django/jaunty-backports

« back to all changes in this revision

Viewing changes to tests/modeltests/reserved_names/models.py

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant
  • Date: 2008-11-15 19:15:33 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20081115191533-xbt1ut2xf4fvwtvc
Tags: 1.0.1-0ubuntu1
* New upstream release:
  - Bug fixes.

* The tests/ sub-directory appaers to have been dropped upstream, so pull
  our patch to workaround the tests and modify the rules.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""
2
 
18. Using SQL reserved names
3
 
 
4
 
Need to use a reserved SQL name as a column name or table name? Need to include
5
 
a hyphen in a column or table name? No problem. Django quotes names
6
 
appropriately behind the scenes, so your database won't complain about
7
 
reserved-name usage.
8
 
"""
9
 
 
10
 
from django.db import models
11
 
 
12
 
class Thing(models.Model):
13
 
    when = models.CharField(max_length=1, primary_key=True)
14
 
    join = models.CharField(max_length=1)
15
 
    like = models.CharField(max_length=1)
16
 
    drop = models.CharField(max_length=1)
17
 
    alter = models.CharField(max_length=1)
18
 
    having = models.CharField(max_length=1)
19
 
    where = models.DateField(max_length=1)
20
 
    has_hyphen = models.CharField(max_length=1, db_column='has-hyphen')
21
 
    class Meta:
22
 
       db_table = 'select'
23
 
 
24
 
    def __unicode__(self):
25
 
        return self.when
26
 
 
27
 
__test__ = {'API_TESTS':"""
28
 
>>> import datetime
29
 
>>> day1 = datetime.date(2005, 1, 1)
30
 
>>> day2 = datetime.date(2006, 2, 2)
31
 
>>> t = Thing(when='a', join='b', like='c', drop='d', alter='e', having='f', where=day1, has_hyphen='h')
32
 
>>> t.save()
33
 
>>> print t.when
34
 
a
35
 
 
36
 
>>> u = Thing(when='h', join='i', like='j', drop='k', alter='l', having='m', where=day2)
37
 
>>> u.save()
38
 
>>> print u.when
39
 
h
40
 
 
41
 
>>> Thing.objects.order_by('when')
42
 
[<Thing: a>, <Thing: h>]
43
 
>>> v = Thing.objects.get(pk='a')
44
 
>>> print v.join
45
 
b
46
 
>>> print v.where
47
 
2005-01-01
48
 
 
49
 
>>> Thing.objects.dates('where', 'year')
50
 
[datetime.datetime(2005, 1, 1, 0, 0), datetime.datetime(2006, 1, 1, 0, 0)]
51
 
 
52
 
>>> Thing.objects.filter(where__month=1)
53
 
[<Thing: a>]
54
 
"""}