Django

Code

root/django/trunk/tests/modeltests/custom_columns/models.py

Revision 8326, 2.9 kB (checked in by gwilson, 2 months ago)

Minor correction for [8325].

  • Property svn:eol-style set to native
Line 
1 """
2 17. Custom column/table names
3
4 If your database column name is different than your model attribute, use the
5 ``db_column`` parameter. Note that you'll use the field's name, not its column
6 name, in API usage.
7
8 If your database table name is different than your model name, use the
9 ``db_table`` Meta attribute. This has no effect on the API used to
10 query the database.
11
12 If you need to use a table name for a many-to-many relationship that differs
13 from the default generated name, use the ``db_table`` parameter on the
14 ``ManyToManyField``. This has no effect on the API for querying the database.
15
16 """
17
18 from django.db import models
19
20 class Author(models.Model):
21     first_name = models.CharField(max_length=30, db_column='firstname')
22     last_name = models.CharField(max_length=30, db_column='last')
23
24     def __unicode__(self):
25         return u'%s %s' % (self.first_name, self.last_name)
26
27     class Meta:
28         db_table = 'my_author_table'
29         ordering = ('last_name','first_name')
30
31 class Article(models.Model):
32     headline = models.CharField(max_length=100)
33     authors = models.ManyToManyField(Author, db_table='my_m2m_table')
34
35     def __unicode__(self):
36         return self.headline
37
38     class Meta:
39         ordering = ('headline',)
40
41 __test__ = {'API_TESTS':"""
42 # Create a Author.
43 >>> a = Author(first_name='John', last_name='Smith')
44 >>> a.save()
45
46 >>> a.id
47 1
48
49 # Create another author
50 >>> a2 = Author(first_name='Peter', last_name='Jones')
51 >>> a2.save()
52
53 # Create an article
54 >>> art = Article(headline='Django lets you build web apps easily')
55 >>> art.save()
56 >>> art.authors = [a, a2]
57
58 # Although the table and column names on Author have been set to custom values,
59 # nothing about using the Author model has changed...
60
61 # Query the available authors
62 >>> Author.objects.all()
63 [<Author: Peter Jones>, <Author: John Smith>]
64
65 >>> Author.objects.filter(first_name__exact='John')
66 [<Author: John Smith>]
67
68 >>> Author.objects.get(first_name__exact='John')
69 <Author: John Smith>
70
71 >>> Author.objects.filter(firstname__exact='John')
72 Traceback (most recent call last):
73     ...
74 FieldError: Cannot resolve keyword 'firstname' into field. Choices are: article, first_name, id, last_name
75
76 >>> a = Author.objects.get(last_name__exact='Smith')
77 >>> a.first_name
78 u'John'
79 >>> a.last_name
80 u'Smith'
81 >>> a.firstname
82 Traceback (most recent call last):
83     ...
84 AttributeError: 'Author' object has no attribute 'firstname'
85 >>> a.last
86 Traceback (most recent call last):
87     ...
88 AttributeError: 'Author' object has no attribute 'last'
89
90 # Although the Article table uses a custom m2m table,
91 # nothing about using the m2m relationship has changed...
92
93 # Get all the authors for an article
94 >>> art.authors.all()
95 [<Author: Peter Jones>, <Author: John Smith>]
96
97 # Get the articles for an author
98 >>> a.article_set.all()
99 [<Article: Django lets you build web apps easily>]
100
101 # Query the authors across the m2m relation
102 >>> art.authors.filter(last_name='Jones')
103 [<Author: Peter Jones>]
104
105 """}
Note: See TracBrowser for help on using the browser.