|
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(maxlength=1, primary_key=True) |
|
14 join = models.CharField(maxlength=1) |
|
15 like = models.CharField(maxlength=1) |
|
16 drop = models.CharField(maxlength=1) |
|
17 alter = models.CharField(maxlength=1) |
|
18 having = models.CharField(maxlength=1) |
|
19 where = models.DateField(maxlength=1) |
|
20 has_hyphen = models.CharField(maxlength=1, db_column='has-hyphen') |
|
21 class Meta: |
|
22 db_table = 'select' |
|
23 |
|
24 def __str__(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 >>> Thing.objects.order_by('select.when') |
|
49 [<Thing: a>, <Thing: h>] |
|
50 |
|
51 >>> Thing.objects.dates('where', 'year') |
|
52 [datetime.datetime(2005, 1, 1, 0, 0), datetime.datetime(2006, 1, 1, 0, 0)] |
|
53 |
|
54 >>> Thing.objects.filter(where__month=1) |
|
55 [<Thing: a>] |
|
56 """} |