2005-07-29 23:15:40 +08:00
|
|
|
"""
|
|
|
|
1. Bare-bones model
|
|
|
|
|
|
|
|
This is a basic model with only two non-primary-key fields.
|
|
|
|
"""
|
|
|
|
|
2005-12-14 13:02:51 +08:00
|
|
|
from django.db import models
|
2005-07-29 23:15:40 +08:00
|
|
|
|
2005-12-14 13:02:51 +08:00
|
|
|
class Article(models.Model):
|
|
|
|
headline = models.CharField(maxlength=100, default='Default headline')
|
|
|
|
pub_date = models.DateTimeField()
|
2005-07-29 23:15:40 +08:00
|
|
|
|
|
|
|
API_TESTS = """
|
2006-01-29 08:23:52 +08:00
|
|
|
|
2005-07-29 23:15:40 +08:00
|
|
|
# No articles are in the system yet.
|
2006-01-31 09:08:02 +08:00
|
|
|
>>> Article.objects.all()
|
2005-07-29 23:15:40 +08:00
|
|
|
[]
|
|
|
|
|
|
|
|
# Create an Article.
|
|
|
|
>>> from datetime import datetime
|
2006-01-30 08:38:23 +08:00
|
|
|
>>> a = Article(id=None, headline='Area man programs in Python', pub_date=datetime(2005, 7, 28))
|
2005-07-29 23:15:40 +08:00
|
|
|
|
|
|
|
# Save it into the database. You have to call save() explicitly.
|
|
|
|
>>> a.save()
|
|
|
|
|
|
|
|
# Now it has an ID. Note it's a long integer, as designated by the trailing "L".
|
|
|
|
>>> a.id
|
|
|
|
1L
|
|
|
|
|
|
|
|
# Access database columns via Python attributes.
|
|
|
|
>>> a.headline
|
|
|
|
'Area man programs in Python'
|
|
|
|
>>> a.pub_date
|
|
|
|
datetime.datetime(2005, 7, 28, 0, 0)
|
|
|
|
|
|
|
|
# Change values by changing the attributes, then calling save().
|
|
|
|
>>> a.headline = 'Area woman programs in Python'
|
|
|
|
>>> a.save()
|
|
|
|
|
2006-01-31 10:21:31 +08:00
|
|
|
# Article.objects.all() returns all the articles in the database. Note that
|
|
|
|
# the article is represented by "<Article object>", because we haven't given
|
|
|
|
# the Article model a __repr__() method.
|
2006-01-31 09:08:02 +08:00
|
|
|
>>> Article.objects.all()
|
2005-07-29 23:15:40 +08:00
|
|
|
[<Article object>]
|
|
|
|
|
2006-01-31 10:21:31 +08:00
|
|
|
# Django provides a rich database lookup API.
|
2006-01-29 08:23:52 +08:00
|
|
|
>>> Article.objects.get(id__exact=1)
|
|
|
|
<Article object>
|
|
|
|
>>> Article.objects.get(headline__startswith='Area woman')
|
|
|
|
<Article object>
|
|
|
|
>>> Article.objects.get(pub_date__year=2005)
|
2005-07-29 23:15:40 +08:00
|
|
|
<Article object>
|
2006-01-29 08:23:52 +08:00
|
|
|
>>> Article.objects.get(pub_date__year=2005, pub_date__month=7)
|
2005-07-29 23:15:40 +08:00
|
|
|
<Article object>
|
2006-01-29 08:23:52 +08:00
|
|
|
>>> Article.objects.get(pub_date__year=2005, pub_date__month=7, pub_date__day=28)
|
2005-07-29 23:15:40 +08:00
|
|
|
<Article object>
|
2006-01-29 08:23:52 +08:00
|
|
|
|
2006-01-31 10:21:31 +08:00
|
|
|
# The "__exact" lookup type can be omitted, as a shortcut.
|
2006-01-29 08:23:52 +08:00
|
|
|
>>> Article.objects.get(id=1)
|
2005-11-01 08:40:32 +08:00
|
|
|
<Article object>
|
2006-01-29 08:23:52 +08:00
|
|
|
>>> Article.objects.get(headline='Area woman programs in Python')
|
2005-11-01 08:40:32 +08:00
|
|
|
<Article object>
|
|
|
|
|
2006-01-31 09:08:02 +08:00
|
|
|
>>> Article.objects.filter(pub_date__year=2005)
|
2005-11-01 08:40:32 +08:00
|
|
|
[<Article object>]
|
2006-01-31 09:08:02 +08:00
|
|
|
>>> Article.objects.filter(pub_date__year=2004)
|
2005-11-01 08:40:32 +08:00
|
|
|
[]
|
2006-01-31 09:08:02 +08:00
|
|
|
>>> Article.objects.filter(pub_date__year=2005, pub_date__month=7)
|
2005-11-01 08:40:32 +08:00
|
|
|
[<Article object>]
|
2005-07-29 23:15:40 +08:00
|
|
|
|
2006-01-31 10:21:31 +08:00
|
|
|
# Django raises an Article.DoesNotExist exception for get() if the parameters
|
|
|
|
# don't match any object.
|
2006-01-29 08:23:52 +08:00
|
|
|
>>> Article.objects.get(id__exact=2)
|
2005-07-29 23:15:40 +08:00
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2005-12-12 06:10:02 +08:00
|
|
|
DoesNotExist: Article does not exist for {'id__exact': 2}
|
2005-07-29 23:15:40 +08:00
|
|
|
|
2006-01-29 08:23:52 +08:00
|
|
|
>>> Article.objects.get(pub_date__year=2005, pub_date__month=8)
|
2005-11-01 08:40:32 +08:00
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2005-12-12 06:10:02 +08:00
|
|
|
DoesNotExist: Article does not exist for ...
|
2005-11-01 08:40:32 +08:00
|
|
|
|
2005-07-29 23:15:40 +08:00
|
|
|
# Lookup by a primary key is the most common case, so Django provides a
|
|
|
|
# shortcut for primary-key exact lookups.
|
2006-01-31 10:21:31 +08:00
|
|
|
# The following is identical to articles.get(id=1).
|
2006-01-29 08:23:52 +08:00
|
|
|
>>> Article.objects.get(pk=1)
|
2005-07-29 23:15:40 +08:00
|
|
|
<Article object>
|
|
|
|
|
2005-07-30 04:29:59 +08:00
|
|
|
# Model instances of the same type and same ID are considered equal.
|
2006-01-29 08:23:52 +08:00
|
|
|
>>> a = Article.objects.get(pk=1)
|
|
|
|
>>> b = Article.objects.get(pk=1)
|
2005-07-30 04:29:59 +08:00
|
|
|
>>> a == b
|
|
|
|
True
|
2005-08-02 00:26:39 +08:00
|
|
|
|
|
|
|
# You can initialize a model instance using positional arguments, which should
|
2006-01-31 10:21:31 +08:00
|
|
|
# match the field order as defined in the model.
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> a2 = Article(None, 'Second article', datetime(2005, 7, 29))
|
2005-08-02 00:26:39 +08:00
|
|
|
>>> a2.save()
|
|
|
|
>>> a2.id
|
|
|
|
2L
|
|
|
|
>>> a2.headline
|
|
|
|
'Second article'
|
|
|
|
>>> a2.pub_date
|
|
|
|
datetime.datetime(2005, 7, 29, 0, 0)
|
|
|
|
|
|
|
|
# ...or, you can use keyword arguments.
|
2006-01-30 08:38:23 +08:00
|
|
|
>>> a3 = Article(id=None, headline='Third article', pub_date=datetime(2005, 7, 30))
|
2005-08-02 00:26:39 +08:00
|
|
|
>>> a3.save()
|
|
|
|
>>> a3.id
|
|
|
|
3L
|
|
|
|
>>> a3.headline
|
|
|
|
'Third article'
|
|
|
|
>>> a3.pub_date
|
|
|
|
datetime.datetime(2005, 7, 30, 0, 0)
|
|
|
|
|
|
|
|
# You can also mix and match position and keyword arguments, but be sure not to
|
|
|
|
# duplicate field information.
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> a4 = Article(None, 'Fourth article', pub_date=datetime(2005, 7, 31))
|
2005-08-02 00:26:39 +08:00
|
|
|
>>> a4.save()
|
|
|
|
>>> a4.headline
|
|
|
|
'Fourth article'
|
|
|
|
|
|
|
|
# Don't use invalid keyword arguments.
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> a5 = Article(id=None, headline='Invalid', pub_date=datetime(2005, 7, 31), foo='bar')
|
2005-08-02 00:26:39 +08:00
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
TypeError: 'foo' is an invalid keyword argument for this function
|
|
|
|
|
2006-01-31 10:21:31 +08:00
|
|
|
# You can leave off the value for an AutoField when creating an object, because
|
|
|
|
# it'll get filled in automatically when you save().
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> a5 = Article(headline='Article 6', pub_date=datetime(2005, 7, 31))
|
2005-08-02 00:26:39 +08:00
|
|
|
>>> a5.save()
|
|
|
|
>>> a5.id
|
|
|
|
5L
|
|
|
|
>>> a5.headline
|
|
|
|
'Article 6'
|
|
|
|
|
|
|
|
# If you leave off a field with "default" set, Django will use the default.
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> a6 = Article(pub_date=datetime(2005, 7, 31))
|
2005-08-02 00:26:39 +08:00
|
|
|
>>> a6.save()
|
|
|
|
>>> a6.headline
|
|
|
|
'Default headline'
|
2005-08-12 03:34:34 +08:00
|
|
|
|
|
|
|
# For DateTimeFields, Django saves as much precision (in seconds) as you
|
|
|
|
# give it.
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> a7 = Article(headline='Article 7', pub_date=datetime(2005, 7, 31, 12, 30))
|
2005-08-12 03:34:34 +08:00
|
|
|
>>> a7.save()
|
2006-01-29 08:23:52 +08:00
|
|
|
>>> Article.objects.get(id__exact=7).pub_date
|
2005-08-12 03:34:34 +08:00
|
|
|
datetime.datetime(2005, 7, 31, 12, 30)
|
|
|
|
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> a8 = Article(headline='Article 8', pub_date=datetime(2005, 7, 31, 12, 30, 45))
|
2005-08-12 03:34:34 +08:00
|
|
|
>>> a8.save()
|
2006-01-29 08:23:52 +08:00
|
|
|
>>> Article.objects.get(id__exact=8).pub_date
|
2005-08-12 03:34:34 +08:00
|
|
|
datetime.datetime(2005, 7, 31, 12, 30, 45)
|
2005-08-15 23:43:59 +08:00
|
|
|
>>> a8.id
|
|
|
|
8L
|
|
|
|
|
2006-01-31 10:21:31 +08:00
|
|
|
# Saving an object again doesn't create a new object -- it just saves the old one.
|
2005-08-15 23:43:59 +08:00
|
|
|
>>> a8.save()
|
|
|
|
>>> a8.id
|
|
|
|
8L
|
|
|
|
>>> a8.headline = 'Updated article 8'
|
|
|
|
>>> a8.save()
|
|
|
|
>>> a8.id
|
|
|
|
8L
|
2005-12-01 11:06:30 +08:00
|
|
|
|
2005-12-05 11:34:35 +08:00
|
|
|
>>> a7 == a8
|
|
|
|
False
|
2006-01-29 08:23:52 +08:00
|
|
|
>>> a8 == Article.objects.get(id__exact=8)
|
2005-12-05 11:34:35 +08:00
|
|
|
True
|
|
|
|
>>> a7 != a8
|
|
|
|
True
|
2006-01-29 08:23:52 +08:00
|
|
|
>>> Article.objects.get(id__exact=8) != Article.objects.get(id__exact=7)
|
2005-12-05 11:37:23 +08:00
|
|
|
True
|
2006-01-29 08:23:52 +08:00
|
|
|
>>> Article.objects.get(id__exact=8) == Article.objects.get(id__exact=7)
|
2005-12-05 11:37:23 +08:00
|
|
|
False
|
2005-12-12 06:57:14 +08:00
|
|
|
|
2006-01-31 10:21:31 +08:00
|
|
|
# dates() returns a list of available dates of the given scope for the given field.
|
2006-01-30 12:36:41 +08:00
|
|
|
>>> Article.objects.dates('pub_date', 'year')
|
2006-01-30 08:38:23 +08:00
|
|
|
[datetime.datetime(2005, 1, 1, 0, 0)]
|
2006-01-30 12:36:41 +08:00
|
|
|
>>> Article.objects.dates('pub_date', 'month')
|
2006-01-30 08:38:23 +08:00
|
|
|
[datetime.datetime(2005, 7, 1, 0, 0)]
|
2006-01-30 12:36:41 +08:00
|
|
|
>>> Article.objects.dates('pub_date', 'day')
|
2006-01-30 08:38:23 +08:00
|
|
|
[datetime.datetime(2005, 7, 28, 0, 0), datetime.datetime(2005, 7, 29, 0, 0), datetime.datetime(2005, 7, 30, 0, 0), datetime.datetime(2005, 7, 31, 0, 0)]
|
2006-01-30 12:36:41 +08:00
|
|
|
>>> Article.objects.dates('pub_date', 'day', order='ASC')
|
2006-01-30 08:38:23 +08:00
|
|
|
[datetime.datetime(2005, 7, 28, 0, 0), datetime.datetime(2005, 7, 29, 0, 0), datetime.datetime(2005, 7, 30, 0, 0), datetime.datetime(2005, 7, 31, 0, 0)]
|
2006-01-30 12:36:41 +08:00
|
|
|
>>> Article.objects.dates('pub_date', 'day', order='DESC')
|
2006-01-30 08:38:23 +08:00
|
|
|
[datetime.datetime(2005, 7, 31, 0, 0), datetime.datetime(2005, 7, 30, 0, 0), datetime.datetime(2005, 7, 29, 0, 0), datetime.datetime(2005, 7, 28, 0, 0)]
|
|
|
|
|
2006-01-31 10:21:31 +08:00
|
|
|
# dates() requires valid arguments.
|
2006-01-30 12:36:41 +08:00
|
|
|
|
|
|
|
>>> Article.objects.dates()
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
TypeError: dates() takes at least 3 arguments (1 given)
|
|
|
|
|
|
|
|
>>> Article.objects.dates('invalid_field', 'year')
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
FieldDoesNotExist: name=invalid_field
|
|
|
|
|
|
|
|
>>> Article.objects.dates('pub_date', 'bad_kind')
|
2006-01-30 08:38:23 +08:00
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
AssertionError: 'kind' must be one of 'year', 'month' or 'day'.
|
2006-01-30 12:36:41 +08:00
|
|
|
|
|
|
|
>>> Article.objects.dates('pub_date', 'year', order='bad order')
|
2006-01-30 08:38:23 +08:00
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2006-01-30 12:36:41 +08:00
|
|
|
AssertionError: 'order' must be either 'ASC' or 'DESC'.
|
2006-01-29 08:23:52 +08:00
|
|
|
|
2006-01-31 10:21:31 +08:00
|
|
|
# Use iterator() with dates() to return a generator that lazily requests each
|
|
|
|
# result one at a time, to save memory.
|
|
|
|
>>> for a in Article.objects.dates('pub_date', 'day', order='DESC').iterator():
|
|
|
|
... print repr(a)
|
|
|
|
datetime.datetime(2005, 7, 31, 0, 0)
|
|
|
|
datetime.datetime(2005, 7, 30, 0, 0)
|
|
|
|
datetime.datetime(2005, 7, 29, 0, 0)
|
|
|
|
datetime.datetime(2005, 7, 28, 0, 0)
|
|
|
|
|
|
|
|
# You can combine queries with & and |.
|
2006-01-29 08:23:52 +08:00
|
|
|
>>> s1 = Article.objects.filter(id__exact=1)
|
|
|
|
>>> s2 = Article.objects.filter(id__exact=2)
|
|
|
|
>>> tmp = [a.id for a in list(s1 | s2)]
|
|
|
|
>>> tmp.sort()
|
|
|
|
>>> tmp
|
|
|
|
[1L, 2L]
|
|
|
|
>>> list(s1 & s2)
|
|
|
|
[]
|
|
|
|
|
|
|
|
# You can get the number of objects like this:
|
|
|
|
>>> len(Article.objects.filter(id__exact=1))
|
|
|
|
1
|
|
|
|
|
2006-01-30 08:38:23 +08:00
|
|
|
# You can get items using index and slice notation.
|
2006-01-31 06:44:05 +08:00
|
|
|
>>> Article.objects.all()[0]
|
2006-01-29 08:23:52 +08:00
|
|
|
<Article object>
|
2006-02-01 06:02:20 +08:00
|
|
|
>>> Article.objects.all()[1:3]
|
2006-01-29 08:23:52 +08:00
|
|
|
[<Article object>, <Article object>]
|
|
|
|
>>> s3 = Article.objects.filter(id__exact=3)
|
|
|
|
>>> (s1 | s2 | s3)[::2]
|
|
|
|
[<Article object>, <Article object>]
|
2006-01-09 22:34:17 +08:00
|
|
|
|
2005-12-14 00:44:54 +08:00
|
|
|
# An Article instance doesn't have access to the "objects" attribute.
|
2006-01-31 10:21:31 +08:00
|
|
|
# That's only available on the class.
|
2006-01-31 06:44:05 +08:00
|
|
|
>>> a7.objects.all()
|
2005-12-14 00:44:54 +08:00
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
AttributeError: Manager isn't accessible via Article instances
|
|
|
|
|
|
|
|
>>> a7.objects
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
AttributeError: Manager isn't accessible via Article instances
|
|
|
|
|
2006-01-17 20:06:16 +08:00
|
|
|
# Bulk delete test: How many objects before and after the delete?
|
2006-01-29 08:23:52 +08:00
|
|
|
>>> Article.objects.count()
|
2006-01-17 20:06:16 +08:00
|
|
|
8L
|
|
|
|
>>> Article.objects.delete(id__lte=4)
|
2006-01-29 08:23:52 +08:00
|
|
|
>>> Article.objects.count()
|
2006-01-17 20:06:16 +08:00
|
|
|
4L
|
|
|
|
|
2006-01-18 21:16:58 +08:00
|
|
|
>>> Article.objects.delete()
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2006-01-23 10:34:07 +08:00
|
|
|
TypeError: SAFETY MECHANISM: Specify DELETE_ALL=True if you actually want to delete all data.
|
2006-01-18 21:16:58 +08:00
|
|
|
|
|
|
|
>>> Article.objects.delete(DELETE_ALL=True)
|
2006-01-29 08:23:52 +08:00
|
|
|
>>> Article.objects.count()
|
2006-01-18 21:16:58 +08:00
|
|
|
0L
|
|
|
|
|
2005-08-12 03:34:34 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
building_docs = getattr(settings, 'BUILDING_DOCS', False)
|
|
|
|
|
|
|
|
if building_docs or settings.DATABASE_ENGINE == 'postgresql':
|
|
|
|
API_TESTS += """
|
|
|
|
# In PostgreSQL, microsecond-level precision is available.
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> a9 = Article(headline='Article 9', pub_date=datetime(2005, 7, 31, 12, 30, 45, 180))
|
2005-08-12 03:34:34 +08:00
|
|
|
>>> a9.save()
|
2006-01-29 08:23:52 +08:00
|
|
|
>>> Article.objects.get(id__exact=9).pub_date
|
2005-08-12 03:34:34 +08:00
|
|
|
datetime.datetime(2005, 7, 31, 12, 30, 45, 180)
|
|
|
|
"""
|
|
|
|
|
|
|
|
if building_docs or settings.DATABASE_ENGINE == 'mysql':
|
|
|
|
API_TESTS += """
|
|
|
|
# In MySQL, microsecond-level precision isn't available. You'll lose
|
|
|
|
# microsecond-level precision once the data is saved.
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> a9 = Article(headline='Article 9', pub_date=datetime(2005, 7, 31, 12, 30, 45, 180))
|
2005-08-12 03:34:34 +08:00
|
|
|
>>> a9.save()
|
2006-01-29 08:23:52 +08:00
|
|
|
>>> Article.objects.get(id__exact=9).pub_date
|
2005-08-12 03:34:34 +08:00
|
|
|
datetime.datetime(2005, 7, 31, 12, 30, 45)
|
2005-07-29 23:15:40 +08:00
|
|
|
"""
|
2005-12-01 14:01:13 +08:00
|
|
|
|
|
|
|
API_TESTS += """
|
|
|
|
|
|
|
|
# You can manually specify the primary key when creating a new objet
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> a101 = Article(id=101, headline='Article 101', pub_date=datetime(2005, 7, 31, 12, 30, 45))
|
2005-12-01 14:01:13 +08:00
|
|
|
>>> a101.save()
|
2006-01-29 08:23:52 +08:00
|
|
|
>>> a101 = Article.objects.get(pk=101)
|
2005-12-01 14:01:13 +08:00
|
|
|
>>> a101.headline
|
|
|
|
'Article 101'
|
|
|
|
"""
|