4 This is a basic model with only two non-primary-key fields.
7 from django
.core
import meta
9 class Article(meta
.Model
):
10 headline
= meta
.CharField(maxlength
=100, default
='Default headline')
11 pub_date
= meta
.DateTimeField()
14 # No articles are in the system yet.
15 >>> articles.get_list()
19 >>> from datetime import datetime
20 >>> a = articles.Article(id=None, headline='Area man programs in Python',
21 ... pub_date=datetime(2005, 7, 28))
23 # Save it into the database. You have to call save() explicitly.
26 # Now it has an ID. Note it's a long integer, as designated by the trailing "L".
30 # Access database columns via Python attributes.
32 'Area man programs in Python'
34 datetime.datetime(2005, 7, 28, 0, 0)
36 # Change values by changing the attributes, then calling save().
37 >>> a.headline = 'Area woman programs in Python'
40 # get_list() displays all the articles in the database. Note that the article
41 # is represented by "<Article object>", because we haven't given the Article
42 # model a __repr__() method.
43 >>> articles.get_list()
46 # Django provides a rich database lookup API that's entirely driven by
48 >>> articles.get_object(id__exact=1)
50 >>> articles.get_object(headline__startswith='Area woman')
52 >>> articles.get_object(pub_date__year=2005)
54 >>> articles.get_object(pub_date__year=2005, pub_date__month=7)
56 >>> articles.get_object(pub_date__year=2005, pub_date__month=7, pub_date__day=28)
59 >>> articles.get_list(pub_date__year=2005)
61 >>> articles.get_list(pub_date__year=2004)
63 >>> articles.get_list(pub_date__year=2005, pub_date__month=7)
66 # Django raises an ArticleDoesNotExist exception for get_object()
67 >>> articles.get_object(id__exact=2)
68 Traceback (most recent call last):
70 ArticleDoesNotExist: Article does not exist for {'id__exact': 2}
72 >>> articles.get_object(pub_date__year=2005, pub_date__month=8)
73 Traceback (most recent call last):
75 ArticleDoesNotExist: Article does not exist for ...
77 # Lookup by a primary key is the most common case, so Django provides a
78 # shortcut for primary-key exact lookups.
79 # The following is identical to articles.get_object(id__exact=1).
80 >>> articles.get_object(pk=1)
83 # Model instances of the same type and same ID are considered equal.
84 >>> a = articles.get_object(pk=1)
85 >>> b = articles.get_object(pk=1)
89 # You can initialize a model instance using positional arguments, which should
90 # match the field order as defined in the model...
91 >>> a2 = articles.Article(None, 'Second article', datetime(2005, 7, 29))
98 datetime.datetime(2005, 7, 29, 0, 0)
100 # ...or, you can use keyword arguments.
101 >>> a3 = articles.Article(id=None, headline='Third article',
102 ... pub_date=datetime(2005, 7, 30))
109 datetime.datetime(2005, 7, 30, 0, 0)
111 # You can also mix and match position and keyword arguments, but be sure not to
112 # duplicate field information.
113 >>> a4 = articles.Article(None, 'Fourth article', pub_date=datetime(2005, 7, 31))
118 # Don't use invalid keyword arguments.
119 >>> a5 = articles.Article(id=None, headline='Invalid', pub_date=datetime(2005, 7, 31), foo='bar')
120 Traceback (most recent call last):
122 TypeError: 'foo' is an invalid keyword argument for this function
124 # You can leave off the ID.
125 >>> a5 = articles.Article(headline='Article 6', pub_date=datetime(2005, 7, 31))
132 # If you leave off a field with "default" set, Django will use the default.
133 >>> a6 = articles.Article(pub_date=datetime(2005, 7, 31))
138 # For DateTimeFields, Django saves as much precision (in seconds) as you
140 >>> a7 = articles.Article(headline='Article 7', pub_date=datetime(2005, 7, 31, 12, 30))
142 >>> articles.get_object(id__exact=7).pub_date
143 datetime.datetime(2005, 7, 31, 12, 30)
145 >>> a8 = articles.Article(headline='Article 8', pub_date=datetime(2005, 7, 31, 12, 30, 45))
147 >>> articles.get_object(id__exact=8).pub_date
148 datetime.datetime(2005, 7, 31, 12, 30, 45)
152 # Saving an object again shouldn't create a new object -- it just saves the old one.
156 >>> a8.headline = 'Updated article 8'
163 >>> a8 == articles.get_object(id__exact=8)
167 >>> articles.get_object(id__exact=8) != articles.get_object(id__exact=7)
169 >>> articles.get_object(id__exact=8) == articles.get_object(id__exact=7)
173 from django
.conf
import settings
175 building_docs
= getattr(settings
, 'BUILDING_DOCS', False)
177 if building_docs
or settings
.DATABASE_ENGINE
== 'postgresql':
179 # In PostgreSQL, microsecond-level precision is available.
180 >>> a9 = articles.Article(headline='Article 9', pub_date=datetime(2005, 7, 31, 12, 30, 45, 180))
182 >>> articles.get_object(id__exact=9).pub_date
183 datetime.datetime(2005, 7, 31, 12, 30, 45, 180)
186 if building_docs
or settings
.DATABASE_ENGINE
== 'mysql':
188 # In MySQL, microsecond-level precision isn't available. You'll lose
189 # microsecond-level precision once the data is saved.
190 >>> a9 = articles.Article(headline='Article 9', pub_date=datetime(2005, 7, 31, 12, 30, 45, 180))
192 >>> articles.get_object(id__exact=9).pub_date
193 datetime.datetime(2005, 7, 31, 12, 30, 45)
198 # You can manually specify the primary key when creating a new objet
199 >>> a101 = articles.Article(id=101, headline='Article 101', pub_date=datetime(2005, 7, 31, 12, 30, 45))
201 >>> a101 = articles.get_object(pk=101)