1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
"""
15. Subclassing models
You can subclass another model to create a copy of it that behaves slightly
differently.
"""
from django.core import meta
# From the "Bare-bones model" example
from django.models.basic import Article
# From the "Adding __repr__()" example
from django.models.repr import Article as ArticleWithRepr
# From the "Specifying ordering" example
from django.models.ordering import Article as ArticleWithOrdering
# This uses all fields and metadata from Article and
# adds a "section" field.
class ArticleWithSection(Article):
section = meta.CharField(maxlength=30)
class META:
module_name = 'subarticles1'
# This uses all fields and metadata from Article but
# removes the "pub_date" field.
class ArticleWithoutPubDate(Article):
class META:
module_name = 'subarticles2'
remove_fields = ('pub_date',)
# This uses all fields and metadata from Article but
# overrides the "pub_date" field.
class ArticleWithFieldOverride(Article):
pub_date = meta.DateField() # overrides the old field, a DateTimeField
class META:
module_name = 'subarticles3'
# No need to add remove_fields = ('pub_date',)
# This uses all fields and metadata from ArticleWithRepr and
# makes a few additions/changes.
class ArticleWithManyChanges(ArticleWithRepr):
section = meta.CharField(maxlength=30)
is_popular = meta.BooleanField()
pub_date = meta.DateField() # overrides the old field, a DateTimeField
class META:
module_name = 'subarticles4'
# This uses all fields from ArticleWithOrdering but
# changes the ordering parameter.
class ArticleWithChangedMeta(ArticleWithOrdering):
class META:
module_name = 'subarticles5'
ordering = ('headline', 'pub_date')
# These two models don't define a module_name.
class NoModuleNameFirst(Article):
section = meta.CharField(maxlength=30)
class NoModuleNameSecond(Article):
section = meta.CharField(maxlength=30)
API_TESTS = """
# No data is in the system yet.
>>> subarticles1.get_list()
[]
>>> subarticles2.get_list()
[]
>>> subarticles3.get_list()
[]
# Create an ArticleWithSection.
>>> from datetime import date, datetime
>>> a1 = subarticles1.ArticleWithSection(headline='First', pub_date=datetime(2005, 8, 22), section='News')
>>> a1.save()
>>> a1
<ArticleWithSection object>
>>> a1.id
1
>>> a1.headline
'First'
>>> a1.pub_date
datetime.datetime(2005, 8, 22, 0, 0)
# Retrieve it again, to prove the fields have been saved.
>>> a1 = subarticles1.get_object(pk=1)
>>> a1.headline
'First'
>>> a1.pub_date
datetime.datetime(2005, 8, 22, 0, 0)
>>> a1.section
'News'
# Create an ArticleWithoutPubDate.
>>> a2 = subarticles2.ArticleWithoutPubDate(headline='Second')
>>> a2.save()
>>> a2
<ArticleWithoutPubDate object>
>>> a2.id
1
>>> a2.pub_date
Traceback (most recent call last):
...
AttributeError: 'ArticleWithoutPubDate' object has no attribute 'pub_date'
# Retrieve it again, to prove the fields have been saved.
>>> a2 = subarticles2.get_object(pk=1)
>>> a2.headline
'Second'
>>> a2.pub_date
Traceback (most recent call last):
...
AttributeError: 'ArticleWithoutPubDate' object has no attribute 'pub_date'
# Create an ArticleWithFieldOverride.
>>> a3 = subarticles3.ArticleWithFieldOverride(headline='Third', pub_date=date(2005, 8, 22))
>>> a3.save()
>>> a3
<ArticleWithFieldOverride object>
>>> a3.id
1
>>> a3.pub_date
datetime.date(2005, 8, 22)
# Retrieve it again, to prove the fields have been saved.
>>> a3 = subarticles3.get_object(pk=1)
>>> a3.headline
'Third'
>>> a3.pub_date
datetime.date(2005, 8, 22)
# Create an ArticleWithManyChanges.
>>> a4 = subarticles4.ArticleWithManyChanges(headline='Fourth', section='Arts',
... is_popular=True, pub_date=date(2005, 8, 22))
>>> a4.save()
# a4 inherits __repr__() from its parent model (ArticleWithRepr).
>>> a4
Fourth
# Retrieve it again, to prove the fields have been saved.
>>> a4 = subarticles4.get_object(pk=1)
>>> a4.headline
'Fourth'
>>> a4.section
'Arts'
>>> a4.is_popular == True
True
>>> a4.pub_date
datetime.date(2005, 8, 22)
# Test get_list().
>>> subarticles1.get_list()
[<ArticleWithSection object>]
>>> subarticles2.get_list()
[<ArticleWithoutPubDate object>]
>>> subarticles3.get_list()
[<ArticleWithFieldOverride object>]
>>> subarticles4.get_list()
[Fourth]
# Create a couple of ArticleWithChangedMeta objects.
>>> a5 = subarticles5.ArticleWithChangedMeta(headline='A', pub_date=datetime(2005, 3, 1))
>>> a5.save()
>>> a6 = subarticles5.ArticleWithChangedMeta(headline='B', pub_date=datetime(2005, 4, 1))
>>> a6.save()
>>> a7 = subarticles5.ArticleWithChangedMeta(headline='C', pub_date=datetime(2005, 5, 1))
>>> a7.save()
# Ordering has been overridden, so objects are ordered
# by headline ASC instead of pub_date DESC.
>>> subarticles5.get_list()
[A, B, C]
>>> nomodulenamefirsts.get_list()
[]
>>> nomodulenameseconds.get_list()
[]
"""
|