mirror of
https://github.com/django/django.git
synced 2025-05-07 15:36:29 +00:00
magic-removal: Changed model unit tests to use new Model.objects.* syntax instead of models.*
git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@1595 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
69b82e1ca1
commit
c1be782510
@ -132,7 +132,7 @@ class TestRunner:
|
|||||||
|
|
||||||
# Run the API tests.
|
# Run the API tests.
|
||||||
p = doctest.DocTestParser()
|
p = doctest.DocTestParser()
|
||||||
test_namespace = dict([(m._meta.module_name, getattr(mod, m._meta.module_name)) for m in mod._MODELS])
|
test_namespace = dict([(m._meta.object_name, getattr(mod, m._meta.module_name).Klass) for m in mod._MODELS])
|
||||||
dtest = p.get_doctest(mod.API_TESTS, test_namespace, model_name, None, None)
|
dtest = p.get_doctest(mod.API_TESTS, test_namespace, model_name, None, None)
|
||||||
# Manually set verbose=False, because "-v" command-line parameter
|
# Manually set verbose=False, because "-v" command-line parameter
|
||||||
# has side effects on doctest TestRunner class.
|
# has side effects on doctest TestRunner class.
|
||||||
|
@ -12,12 +12,12 @@ class Article(meta.Model):
|
|||||||
|
|
||||||
API_TESTS = """
|
API_TESTS = """
|
||||||
# No articles are in the system yet.
|
# No articles are in the system yet.
|
||||||
>>> articles.get_list()
|
>>> Article.objects.get_list()
|
||||||
[]
|
[]
|
||||||
|
|
||||||
# Create an Article.
|
# Create an Article.
|
||||||
>>> from datetime import datetime
|
>>> from datetime import datetime
|
||||||
>>> a = articles.Article(id=None, headline='Area man programs in Python',
|
>>> a = Article(id=None, headline='Area man programs in Python',
|
||||||
... pub_date=datetime(2005, 7, 28))
|
... pub_date=datetime(2005, 7, 28))
|
||||||
|
|
||||||
# Save it into the database. You have to call save() explicitly.
|
# Save it into the database. You have to call save() explicitly.
|
||||||
@ -40,55 +40,55 @@ datetime.datetime(2005, 7, 28, 0, 0)
|
|||||||
# get_list() displays all the articles in the database. Note that the article
|
# get_list() displays all the articles in the database. Note that the article
|
||||||
# is represented by "<Article object>", because we haven't given the Article
|
# is represented by "<Article object>", because we haven't given the Article
|
||||||
# model a __repr__() method.
|
# model a __repr__() method.
|
||||||
>>> articles.get_list()
|
>>> Article.objects.get_list()
|
||||||
[<Article object>]
|
[<Article object>]
|
||||||
|
|
||||||
# Django provides a rich database lookup API that's entirely driven by
|
# Django provides a rich database lookup API that's entirely driven by
|
||||||
# keyword arguments.
|
# keyword arguments.
|
||||||
>>> articles.get_object(id__exact=1)
|
>>> Article.objects.get_object(id__exact=1)
|
||||||
<Article object>
|
<Article object>
|
||||||
>>> articles.get_object(headline__startswith='Area woman')
|
>>> Article.objects.get_object(headline__startswith='Area woman')
|
||||||
<Article object>
|
<Article object>
|
||||||
>>> articles.get_object(pub_date__year=2005)
|
>>> Article.objects.get_object(pub_date__year=2005)
|
||||||
<Article object>
|
<Article object>
|
||||||
>>> articles.get_object(pub_date__year=2005, pub_date__month=7)
|
>>> Article.objects.get_object(pub_date__year=2005, pub_date__month=7)
|
||||||
<Article object>
|
<Article object>
|
||||||
>>> articles.get_object(pub_date__year=2005, pub_date__month=7, pub_date__day=28)
|
>>> Article.objects.get_object(pub_date__year=2005, pub_date__month=7, pub_date__day=28)
|
||||||
<Article object>
|
<Article object>
|
||||||
|
|
||||||
>>> articles.get_list(pub_date__year=2005)
|
>>> Article.objects.get_list(pub_date__year=2005)
|
||||||
[<Article object>]
|
[<Article object>]
|
||||||
>>> articles.get_list(pub_date__year=2004)
|
>>> Article.objects.get_list(pub_date__year=2004)
|
||||||
[]
|
[]
|
||||||
>>> articles.get_list(pub_date__year=2005, pub_date__month=7)
|
>>> Article.objects.get_list(pub_date__year=2005, pub_date__month=7)
|
||||||
[<Article object>]
|
[<Article object>]
|
||||||
|
|
||||||
# Django raises an ArticleDoesNotExist exception for get_object()
|
# Django raises an ArticleDoesNotExist exception for get_object()
|
||||||
>>> articles.get_object(id__exact=2)
|
>>> Article.objects.get_object(id__exact=2)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ArticleDoesNotExist: Article does not exist for {'id__exact': 2}
|
DoesNotExist: Article does not exist for {'id__exact': 2}
|
||||||
|
|
||||||
>>> articles.get_object(pub_date__year=2005, pub_date__month=8)
|
>>> Article.objects.get_object(pub_date__year=2005, pub_date__month=8)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ArticleDoesNotExist: Article does not exist for ...
|
DoesNotExist: Article does not exist for ...
|
||||||
|
|
||||||
# Lookup by a primary key is the most common case, so Django provides a
|
# Lookup by a primary key is the most common case, so Django provides a
|
||||||
# shortcut for primary-key exact lookups.
|
# shortcut for primary-key exact lookups.
|
||||||
# The following is identical to articles.get_object(id__exact=1).
|
# The following is identical to articles.get_object(id__exact=1).
|
||||||
>>> articles.get_object(pk=1)
|
>>> Article.objects.get_object(pk=1)
|
||||||
<Article object>
|
<Article object>
|
||||||
|
|
||||||
# Model instances of the same type and same ID are considered equal.
|
# Model instances of the same type and same ID are considered equal.
|
||||||
>>> a = articles.get_object(pk=1)
|
>>> a = Article.objects.get_object(pk=1)
|
||||||
>>> b = articles.get_object(pk=1)
|
>>> b = Article.objects.get_object(pk=1)
|
||||||
>>> a == b
|
>>> a == b
|
||||||
True
|
True
|
||||||
|
|
||||||
# You can initialize a model instance using positional arguments, which should
|
# You can initialize a model instance using positional arguments, which should
|
||||||
# match the field order as defined in the model...
|
# match the field order as defined in the model...
|
||||||
>>> a2 = articles.Article(None, 'Second article', datetime(2005, 7, 29))
|
>>> a2 = Article(None, 'Second article', datetime(2005, 7, 29))
|
||||||
>>> a2.save()
|
>>> a2.save()
|
||||||
>>> a2.id
|
>>> a2.id
|
||||||
2L
|
2L
|
||||||
@ -98,7 +98,7 @@ True
|
|||||||
datetime.datetime(2005, 7, 29, 0, 0)
|
datetime.datetime(2005, 7, 29, 0, 0)
|
||||||
|
|
||||||
# ...or, you can use keyword arguments.
|
# ...or, you can use keyword arguments.
|
||||||
>>> a3 = articles.Article(id=None, headline='Third article',
|
>>> a3 = Article(id=None, headline='Third article',
|
||||||
... pub_date=datetime(2005, 7, 30))
|
... pub_date=datetime(2005, 7, 30))
|
||||||
>>> a3.save()
|
>>> a3.save()
|
||||||
>>> a3.id
|
>>> a3.id
|
||||||
@ -110,19 +110,19 @@ datetime.datetime(2005, 7, 30, 0, 0)
|
|||||||
|
|
||||||
# You can also mix and match position and keyword arguments, but be sure not to
|
# You can also mix and match position and keyword arguments, but be sure not to
|
||||||
# duplicate field information.
|
# duplicate field information.
|
||||||
>>> a4 = articles.Article(None, 'Fourth article', pub_date=datetime(2005, 7, 31))
|
>>> a4 = Article(None, 'Fourth article', pub_date=datetime(2005, 7, 31))
|
||||||
>>> a4.save()
|
>>> a4.save()
|
||||||
>>> a4.headline
|
>>> a4.headline
|
||||||
'Fourth article'
|
'Fourth article'
|
||||||
|
|
||||||
# Don't use invalid keyword arguments.
|
# Don't use invalid keyword arguments.
|
||||||
>>> a5 = articles.Article(id=None, headline='Invalid', pub_date=datetime(2005, 7, 31), foo='bar')
|
>>> a5 = Article(id=None, headline='Invalid', pub_date=datetime(2005, 7, 31), foo='bar')
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
TypeError: 'foo' is an invalid keyword argument for this function
|
TypeError: 'foo' is an invalid keyword argument for this function
|
||||||
|
|
||||||
# You can leave off the ID.
|
# You can leave off the ID.
|
||||||
>>> a5 = articles.Article(headline='Article 6', pub_date=datetime(2005, 7, 31))
|
>>> a5 = Article(headline='Article 6', pub_date=datetime(2005, 7, 31))
|
||||||
>>> a5.save()
|
>>> a5.save()
|
||||||
>>> a5.id
|
>>> a5.id
|
||||||
5L
|
5L
|
||||||
@ -130,21 +130,21 @@ TypeError: 'foo' is an invalid keyword argument for this function
|
|||||||
'Article 6'
|
'Article 6'
|
||||||
|
|
||||||
# If you leave off a field with "default" set, Django will use the default.
|
# If you leave off a field with "default" set, Django will use the default.
|
||||||
>>> a6 = articles.Article(pub_date=datetime(2005, 7, 31))
|
>>> a6 = Article(pub_date=datetime(2005, 7, 31))
|
||||||
>>> a6.save()
|
>>> a6.save()
|
||||||
>>> a6.headline
|
>>> a6.headline
|
||||||
'Default headline'
|
'Default headline'
|
||||||
|
|
||||||
# For DateTimeFields, Django saves as much precision (in seconds) as you
|
# For DateTimeFields, Django saves as much precision (in seconds) as you
|
||||||
# give it.
|
# give it.
|
||||||
>>> a7 = articles.Article(headline='Article 7', pub_date=datetime(2005, 7, 31, 12, 30))
|
>>> a7 = Article(headline='Article 7', pub_date=datetime(2005, 7, 31, 12, 30))
|
||||||
>>> a7.save()
|
>>> a7.save()
|
||||||
>>> articles.get_object(id__exact=7).pub_date
|
>>> Article.objects.get_object(id__exact=7).pub_date
|
||||||
datetime.datetime(2005, 7, 31, 12, 30)
|
datetime.datetime(2005, 7, 31, 12, 30)
|
||||||
|
|
||||||
>>> a8 = articles.Article(headline='Article 8', pub_date=datetime(2005, 7, 31, 12, 30, 45))
|
>>> a8 = Article(headline='Article 8', pub_date=datetime(2005, 7, 31, 12, 30, 45))
|
||||||
>>> a8.save()
|
>>> a8.save()
|
||||||
>>> articles.get_object(id__exact=8).pub_date
|
>>> Article.objects.get_object(id__exact=8).pub_date
|
||||||
datetime.datetime(2005, 7, 31, 12, 30, 45)
|
datetime.datetime(2005, 7, 31, 12, 30, 45)
|
||||||
>>> a8.id
|
>>> a8.id
|
||||||
8L
|
8L
|
||||||
@ -160,13 +160,13 @@ datetime.datetime(2005, 7, 31, 12, 30, 45)
|
|||||||
|
|
||||||
>>> a7 == a8
|
>>> a7 == a8
|
||||||
False
|
False
|
||||||
>>> a8 == articles.get_object(id__exact=8)
|
>>> a8 == Article.objects.get_object(id__exact=8)
|
||||||
True
|
True
|
||||||
>>> a7 != a8
|
>>> a7 != a8
|
||||||
True
|
True
|
||||||
>>> articles.get_object(id__exact=8) != articles.get_object(id__exact=7)
|
>>> Article.objects.get_object(id__exact=8) != articles.get_object(id__exact=7)
|
||||||
True
|
True
|
||||||
>>> articles.get_object(id__exact=8) == articles.get_object(id__exact=7)
|
>>> Article.objects.get_object(id__exact=8) == articles.get_object(id__exact=7)
|
||||||
False
|
False
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -177,9 +177,9 @@ building_docs = getattr(settings, 'BUILDING_DOCS', False)
|
|||||||
if building_docs or settings.DATABASE_ENGINE == 'postgresql':
|
if building_docs or settings.DATABASE_ENGINE == 'postgresql':
|
||||||
API_TESTS += """
|
API_TESTS += """
|
||||||
# In PostgreSQL, microsecond-level precision is available.
|
# In PostgreSQL, microsecond-level precision is available.
|
||||||
>>> a9 = articles.Article(headline='Article 9', pub_date=datetime(2005, 7, 31, 12, 30, 45, 180))
|
>>> a9 = Article(headline='Article 9', pub_date=datetime(2005, 7, 31, 12, 30, 45, 180))
|
||||||
>>> a9.save()
|
>>> a9.save()
|
||||||
>>> articles.get_object(id__exact=9).pub_date
|
>>> Article.objects.get_object(id__exact=9).pub_date
|
||||||
datetime.datetime(2005, 7, 31, 12, 30, 45, 180)
|
datetime.datetime(2005, 7, 31, 12, 30, 45, 180)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -187,18 +187,18 @@ if building_docs or settings.DATABASE_ENGINE == 'mysql':
|
|||||||
API_TESTS += """
|
API_TESTS += """
|
||||||
# In MySQL, microsecond-level precision isn't available. You'll lose
|
# In MySQL, microsecond-level precision isn't available. You'll lose
|
||||||
# microsecond-level precision once the data is saved.
|
# microsecond-level precision once the data is saved.
|
||||||
>>> a9 = articles.Article(headline='Article 9', pub_date=datetime(2005, 7, 31, 12, 30, 45, 180))
|
>>> a9 = Article(headline='Article 9', pub_date=datetime(2005, 7, 31, 12, 30, 45, 180))
|
||||||
>>> a9.save()
|
>>> a9.save()
|
||||||
>>> articles.get_object(id__exact=9).pub_date
|
>>> Article.objects.get_object(id__exact=9).pub_date
|
||||||
datetime.datetime(2005, 7, 31, 12, 30, 45)
|
datetime.datetime(2005, 7, 31, 12, 30, 45)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
API_TESTS += """
|
API_TESTS += """
|
||||||
|
|
||||||
# You can manually specify the primary key when creating a new objet
|
# You can manually specify the primary key when creating a new objet
|
||||||
>>> a101 = articles.Article(id=101, headline='Article 101', pub_date=datetime(2005, 7, 31, 12, 30, 45))
|
>>> a101 = Article(id=101, headline='Article 101', pub_date=datetime(2005, 7, 31, 12, 30, 45))
|
||||||
>>> a101.save()
|
>>> a101.save()
|
||||||
>>> a101 = articles.get_object(pk=101)
|
>>> a101 = Article.objects.get_object(pk=101)
|
||||||
>>> a101.headline
|
>>> a101.headline
|
||||||
'Article 101'
|
'Article 101'
|
||||||
"""
|
"""
|
||||||
|
@ -17,27 +17,27 @@ class Person(meta.Model):
|
|||||||
|
|
||||||
API_TESTS = """
|
API_TESTS = """
|
||||||
# Create a Person.
|
# Create a Person.
|
||||||
>>> p = persons.Person(first_name='John', last_name='Smith')
|
>>> p = Person(first_name='John', last_name='Smith')
|
||||||
>>> p.save()
|
>>> p.save()
|
||||||
|
|
||||||
>>> p.id
|
>>> p.id
|
||||||
1
|
1
|
||||||
|
|
||||||
>>> persons.get_list()
|
>>> Person.objects.get_list()
|
||||||
[John Smith]
|
[John Smith]
|
||||||
|
|
||||||
>>> persons.get_list(first_name__exact='John')
|
>>> Person.objects.get_list(first_name__exact='John')
|
||||||
[John Smith]
|
[John Smith]
|
||||||
|
|
||||||
>>> persons.get_object(first_name__exact='John')
|
>>> Person.objects.get_object(first_name__exact='John')
|
||||||
John Smith
|
John Smith
|
||||||
|
|
||||||
>>> persons.get_list(firstname__exact='John')
|
>>> Person.objects.get_list(firstname__exact='John')
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
TypeError: got unexpected keyword argument 'firstname__exact'
|
TypeError: got unexpected keyword argument 'firstname__exact'
|
||||||
|
|
||||||
>>> p = persons.get_object(last_name__exact='Smith')
|
>>> p = Person.objects.get_object(last_name__exact='Smith')
|
||||||
>>> p.first_name
|
>>> p.first_name
|
||||||
'John'
|
'John'
|
||||||
>>> p.last_name
|
>>> p.last_name
|
||||||
|
@ -53,9 +53,9 @@ class Article(meta.Model):
|
|||||||
API_TESTS = """
|
API_TESTS = """
|
||||||
# Create a couple of Articles.
|
# Create a couple of Articles.
|
||||||
>>> from datetime import date
|
>>> from datetime import date
|
||||||
>>> a = articles.Article(id=None, headline='Area man programs in Python', pub_date=date(2005, 7, 27))
|
>>> a = Article(id=None, headline='Area man programs in Python', pub_date=date(2005, 7, 27))
|
||||||
>>> a.save()
|
>>> a.save()
|
||||||
>>> b = articles.Article(id=None, headline='Beatles reunite', pub_date=date(2005, 7, 27))
|
>>> b = Article(id=None, headline='Beatles reunite', pub_date=date(2005, 7, 27))
|
||||||
>>> b.save()
|
>>> b.save()
|
||||||
|
|
||||||
# Test the custom methods.
|
# Test the custom methods.
|
||||||
|
@ -28,35 +28,35 @@ class Business(meta.Model):
|
|||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
API_TESTS = """
|
API_TESTS = """
|
||||||
>>> dan = employees.Employee(employee_code='ABC123', first_name='Dan', last_name='Jones')
|
>>> dan = Employee(employee_code='ABC123', first_name='Dan', last_name='Jones')
|
||||||
>>> dan.save()
|
>>> dan.save()
|
||||||
>>> employees.get_list()
|
>>> Employee.objects.get_list()
|
||||||
[Dan Jones]
|
[Dan Jones]
|
||||||
|
|
||||||
>>> fran = employees.Employee(employee_code='XYZ456', first_name='Fran', last_name='Bones')
|
>>> fran = Employee(employee_code='XYZ456', first_name='Fran', last_name='Bones')
|
||||||
>>> fran.save()
|
>>> fran.save()
|
||||||
>>> employees.get_list()
|
>>> Employee.objects.get_list()
|
||||||
[Fran Bones, Dan Jones]
|
[Fran Bones, Dan Jones]
|
||||||
|
|
||||||
>>> employees.get_object(pk='ABC123')
|
>>> Employee.objects.get_object(pk='ABC123')
|
||||||
Dan Jones
|
Dan Jones
|
||||||
>>> employees.get_object(pk='XYZ456')
|
>>> Employee.objects.get_object(pk='XYZ456')
|
||||||
Fran Bones
|
Fran Bones
|
||||||
>>> employees.get_object(pk='foo')
|
>>> Employee.objects.get_object(pk='foo')
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
EmployeeDoesNotExist: Employee does not exist for {'pk': 'foo'}
|
EmployeeDoesNotExist: Employee does not exist for {'pk': 'foo'}
|
||||||
|
|
||||||
# Fran got married and changed her last name.
|
# Fran got married and changed her last name.
|
||||||
>>> fran = employees.get_object(pk='XYZ456')
|
>>> fran = Employee.objects.get_object(pk='XYZ456')
|
||||||
>>> fran.last_name = 'Jones'
|
>>> fran.last_name = 'Jones'
|
||||||
>>> fran.save()
|
>>> fran.save()
|
||||||
>>> employees.get_list(last_name__exact='Jones')
|
>>> Employee.objects.get_list(last_name__exact='Jones')
|
||||||
[Dan Jones, Fran Jones]
|
[Dan Jones, Fran Jones]
|
||||||
>>> employees.get_in_bulk(['ABC123', 'XYZ456'])
|
>>> Employee.objects.get_in_bulk(['ABC123', 'XYZ456'])
|
||||||
{'XYZ456': Fran Jones, 'ABC123': Dan Jones}
|
{'XYZ456': Fran Jones, 'ABC123': Dan Jones}
|
||||||
|
|
||||||
>>> b = businesses.Business(name='Sears')
|
>>> b = Business(name='Sears')
|
||||||
>>> b.save()
|
>>> b.save()
|
||||||
>>> b.set_employees([dan.employee_code, fran.employee_code])
|
>>> b.set_employees([dan.employee_code, fran.employee_code])
|
||||||
True
|
True
|
||||||
@ -64,6 +64,6 @@ True
|
|||||||
[Dan Jones, Fran Jones]
|
[Dan Jones, Fran Jones]
|
||||||
>>> fran.get_business_list()
|
>>> fran.get_business_list()
|
||||||
[Sears]
|
[Sears]
|
||||||
>>> businesses.get_in_bulk(['Sears'])
|
>>> Business.objects.get_in_bulk(['Sears'])
|
||||||
{'Sears': Sears}
|
{'Sears': Sears}
|
||||||
"""
|
"""
|
||||||
|
@ -21,23 +21,23 @@ class Article(meta.Model):
|
|||||||
|
|
||||||
API_TESTS = """
|
API_TESTS = """
|
||||||
# Because no Articles exist yet, get_latest() raises ArticleDoesNotExist.
|
# Because no Articles exist yet, get_latest() raises ArticleDoesNotExist.
|
||||||
>>> articles.get_latest()
|
>>> Article.objects.get_latest()
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ArticleDoesNotExist: Article does not exist for {'order_by': ('-pub_date',), 'limit': 1}
|
DoesNotExist: Article does not exist for {'order_by': ('-pub_date',), 'limit': 1}
|
||||||
|
|
||||||
# Create a couple of Articles.
|
# Create a couple of Articles.
|
||||||
>>> from datetime import datetime
|
>>> from datetime import datetime
|
||||||
>>> a1 = articles.Article(id=None, headline='Article 1', pub_date=datetime(2005, 7, 26))
|
>>> a1 = Article(id=None, headline='Article 1', pub_date=datetime(2005, 7, 26))
|
||||||
>>> a1.save()
|
>>> a1.save()
|
||||||
>>> a2 = articles.Article(id=None, headline='Article 2', pub_date=datetime(2005, 7, 27))
|
>>> a2 = Article(id=None, headline='Article 2', pub_date=datetime(2005, 7, 27))
|
||||||
>>> a2.save()
|
>>> a2.save()
|
||||||
>>> a3 = articles.Article(id=None, headline='Article 3', pub_date=datetime(2005, 7, 27))
|
>>> a3 = Article(id=None, headline='Article 3', pub_date=datetime(2005, 7, 27))
|
||||||
>>> a3.save()
|
>>> a3.save()
|
||||||
>>> a4 = articles.Article(id=None, headline='Article 4', pub_date=datetime(2005, 7, 28))
|
>>> a4 = Article(id=None, headline='Article 4', pub_date=datetime(2005, 7, 28))
|
||||||
>>> a4.save()
|
>>> a4.save()
|
||||||
|
|
||||||
# Get the latest Article.
|
# Get the latest Article.
|
||||||
>>> articles.get_latest()
|
>>> Article.objects.get_latest()
|
||||||
Article 4
|
Article 4
|
||||||
"""
|
"""
|
||||||
|
@ -18,23 +18,23 @@ class Article(meta.Model):
|
|||||||
API_TESTS = """
|
API_TESTS = """
|
||||||
# Create a couple of Articles.
|
# Create a couple of Articles.
|
||||||
>>> from datetime import datetime
|
>>> from datetime import datetime
|
||||||
>>> a1 = articles.Article(headline='Article 1', pub_date=datetime(2005, 7, 26))
|
>>> a1 = Article(headline='Article 1', pub_date=datetime(2005, 7, 26))
|
||||||
>>> a1.save()
|
>>> a1.save()
|
||||||
>>> a2 = articles.Article(headline='Article 2', pub_date=datetime(2005, 7, 27))
|
>>> a2 = Article(headline='Article 2', pub_date=datetime(2005, 7, 27))
|
||||||
>>> a2.save()
|
>>> a2.save()
|
||||||
>>> a3 = articles.Article(headline='Article 3', pub_date=datetime(2005, 7, 27))
|
>>> a3 = Article(headline='Article 3', pub_date=datetime(2005, 7, 27))
|
||||||
>>> a3.save()
|
>>> a3.save()
|
||||||
>>> a4 = articles.Article(headline='Article 4', pub_date=datetime(2005, 7, 28))
|
>>> a4 = Article(headline='Article 4', pub_date=datetime(2005, 7, 28))
|
||||||
>>> a4.save()
|
>>> a4.save()
|
||||||
>>> a5 = articles.Article(headline='Article 5', pub_date=datetime(2005, 8, 1, 9, 0))
|
>>> a5 = Article(headline='Article 5', pub_date=datetime(2005, 8, 1, 9, 0))
|
||||||
>>> a5.save()
|
>>> a5.save()
|
||||||
>>> a6 = articles.Article(headline='Article 6', pub_date=datetime(2005, 8, 1, 8, 0))
|
>>> a6 = Article(headline='Article 6', pub_date=datetime(2005, 8, 1, 8, 0))
|
||||||
>>> a6.save()
|
>>> a6.save()
|
||||||
>>> a7 = articles.Article(headline='Article 7', pub_date=datetime(2005, 7, 27))
|
>>> a7 = Article(headline='Article 7', pub_date=datetime(2005, 7, 27))
|
||||||
>>> a7.save()
|
>>> a7.save()
|
||||||
|
|
||||||
# get_iterator() is just like get_list(), but it's a generator.
|
# get_iterator() is just like get_list(), but it's a generator.
|
||||||
>>> for a in articles.get_iterator():
|
>>> for a in Article.objects.get_iterator():
|
||||||
... print a.headline
|
... print a.headline
|
||||||
Article 5
|
Article 5
|
||||||
Article 6
|
Article 6
|
||||||
@ -45,39 +45,39 @@ Article 7
|
|||||||
Article 1
|
Article 1
|
||||||
|
|
||||||
# get_iterator() takes the same lookup arguments as get_list().
|
# get_iterator() takes the same lookup arguments as get_list().
|
||||||
>>> for a in articles.get_iterator(headline__endswith='4'):
|
>>> for a in Article.objects.get_iterator(headline__endswith='4'):
|
||||||
... print a.headline
|
... print a.headline
|
||||||
Article 4
|
Article 4
|
||||||
|
|
||||||
# get_count() returns the number of objects matching search criteria.
|
# get_count() returns the number of objects matching search criteria.
|
||||||
>>> articles.get_count()
|
>>> Article.objects.get_count()
|
||||||
7L
|
7L
|
||||||
>>> articles.get_count(pub_date__exact=datetime(2005, 7, 27))
|
>>> Article.objects.get_count(pub_date__exact=datetime(2005, 7, 27))
|
||||||
3L
|
3L
|
||||||
>>> articles.get_count(headline__startswith='Blah blah')
|
>>> Article.objects.get_count(headline__startswith='Blah blah')
|
||||||
0L
|
0L
|
||||||
|
|
||||||
# get_in_bulk() takes a list of IDs and returns a dictionary mapping IDs
|
# get_in_bulk() takes a list of IDs and returns a dictionary mapping IDs
|
||||||
# to objects.
|
# to objects.
|
||||||
>>> articles.get_in_bulk([1, 2])
|
>>> Article.objects.get_in_bulk([1, 2])
|
||||||
{1: Article 1, 2: Article 2}
|
{1: Article 1, 2: Article 2}
|
||||||
>>> articles.get_in_bulk([3])
|
>>> Article.objects.get_in_bulk([3])
|
||||||
{3: Article 3}
|
{3: Article 3}
|
||||||
>>> articles.get_in_bulk([1000])
|
>>> Article.objects.get_in_bulk([1000])
|
||||||
{}
|
{}
|
||||||
|
|
||||||
# get_values() is just like get_list(), except it returns a list of
|
# get_values() is just like get_list(), except it returns a list of
|
||||||
# dictionaries instead of object instances -- and you can specify which fields
|
# dictionaries instead of object instances -- and you can specify which fields
|
||||||
# you want to retrieve.
|
# you want to retrieve.
|
||||||
>>> articles.get_values(fields=['headline'])
|
>>> Article.objects.get_values(fields=['headline'])
|
||||||
[{'headline': 'Article 5'}, {'headline': 'Article 6'}, {'headline': 'Article 4'}, {'headline': 'Article 2'}, {'headline': 'Article 3'}, {'headline': 'Article 7'}, {'headline': 'Article 1'}]
|
[{'headline': 'Article 5'}, {'headline': 'Article 6'}, {'headline': 'Article 4'}, {'headline': 'Article 2'}, {'headline': 'Article 3'}, {'headline': 'Article 7'}, {'headline': 'Article 1'}]
|
||||||
>>> articles.get_values(pub_date__exact=datetime(2005, 7, 27), fields=['id'])
|
>>> Article.objects.get_values(pub_date__exact=datetime(2005, 7, 27), fields=['id'])
|
||||||
[{'id': 2}, {'id': 3}, {'id': 7}]
|
[{'id': 2}, {'id': 3}, {'id': 7}]
|
||||||
>>> articles.get_values(fields=['id', 'headline']) == [{'id': 5, 'headline': 'Article 5'}, {'id': 6, 'headline': 'Article 6'}, {'id': 4, 'headline': 'Article 4'}, {'id': 2, 'headline': 'Article 2'}, {'id': 3, 'headline': 'Article 3'}, {'id': 7, 'headline': 'Article 7'}, {'id': 1, 'headline': 'Article 1'}]
|
>>> Article.objects.get_values(fields=['id', 'headline']) == [{'id': 5, 'headline': 'Article 5'}, {'id': 6, 'headline': 'Article 6'}, {'id': 4, 'headline': 'Article 4'}, {'id': 2, 'headline': 'Article 2'}, {'id': 3, 'headline': 'Article 3'}, {'id': 7, 'headline': 'Article 7'}, {'id': 1, 'headline': 'Article 1'}]
|
||||||
True
|
True
|
||||||
|
|
||||||
# get_values_iterator() is just like get_values(), but it's a generator.
|
# get_values_iterator() is just like get_values(), but it's a generator.
|
||||||
>>> for d in articles.get_values_iterator(fields=['id', 'headline']):
|
>>> for d in Article.objects.get_values_iterator(fields=['id', 'headline']):
|
||||||
... i = d.items()
|
... i = d.items()
|
||||||
... i.sort()
|
... i.sort()
|
||||||
... i
|
... i
|
||||||
@ -104,7 +104,7 @@ Article 6
|
|||||||
>>> a5.get_next_by_pub_date()
|
>>> a5.get_next_by_pub_date()
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ArticleDoesNotExist: Article does not exist for ...
|
DoesNotExist: Article does not exist for ...
|
||||||
>>> a6.get_next_by_pub_date()
|
>>> a6.get_next_by_pub_date()
|
||||||
Article 5
|
Article 5
|
||||||
>>> a7.get_next_by_pub_date()
|
>>> a7.get_next_by_pub_date()
|
||||||
@ -125,16 +125,16 @@ Article 1
|
|||||||
|
|
||||||
# Underscores and percent signs have special meaning in the underlying
|
# Underscores and percent signs have special meaning in the underlying
|
||||||
# database library, but Django handles the quoting of them automatically.
|
# database library, but Django handles the quoting of them automatically.
|
||||||
>>> a8 = articles.Article(headline='Article_ with underscore', pub_date=datetime(2005, 11, 20))
|
>>> a8 = Article(headline='Article_ with underscore', pub_date=datetime(2005, 11, 20))
|
||||||
>>> a8.save()
|
>>> a8.save()
|
||||||
>>> articles.get_list(headline__startswith='Article')
|
>>> Article.objects.get_list(headline__startswith='Article')
|
||||||
[Article_ with underscore, Article 5, Article 6, Article 4, Article 2, Article 3, Article 7, Article 1]
|
[Article_ with underscore, Article 5, Article 6, Article 4, Article 2, Article 3, Article 7, Article 1]
|
||||||
>>> articles.get_list(headline__startswith='Article_')
|
>>> Article.objects.get_list(headline__startswith='Article_')
|
||||||
[Article_ with underscore]
|
[Article_ with underscore]
|
||||||
>>> a9 = articles.Article(headline='Article% with percent sign', pub_date=datetime(2005, 11, 21))
|
>>> a9 = Article(headline='Article% with percent sign', pub_date=datetime(2005, 11, 21))
|
||||||
>>> a9.save()
|
>>> a9.save()
|
||||||
>>> articles.get_list(headline__startswith='Article')
|
>>> Article.objects.get_list(headline__startswith='Article')
|
||||||
[Article% with percent sign, Article_ with underscore, Article 5, Article 6, Article 4, Article 2, Article 3, Article 7, Article 1]
|
[Article% with percent sign, Article_ with underscore, Article 5, Article 6, Article 4, Article 2, Article 3, Article 7, Article 1]
|
||||||
>>> articles.get_list(headline__startswith='Article%')
|
>>> Article.objects.get_list(headline__startswith='Article%')
|
||||||
[Article% with percent sign]
|
[Article% with percent sign]
|
||||||
"""
|
"""
|
||||||
|
@ -36,20 +36,20 @@ class Writer(meta.Model):
|
|||||||
|
|
||||||
API_TESTS = """
|
API_TESTS = """
|
||||||
# Create a few Reporters.
|
# Create a few Reporters.
|
||||||
>>> r1 = reporters.Reporter(first_name='John', last_name='Smith')
|
>>> r1 = Reporter(first_name='John', last_name='Smith')
|
||||||
>>> r1.save()
|
>>> r1.save()
|
||||||
>>> r2 = reporters.Reporter(first_name='Jane', last_name='Doe')
|
>>> r2 = Reporter(first_name='Jane', last_name='Doe')
|
||||||
>>> r2.save()
|
>>> r2.save()
|
||||||
|
|
||||||
# Create an Article.
|
# Create an Article.
|
||||||
>>> from datetime import datetime
|
>>> from datetime import datetime
|
||||||
>>> a = articles.Article(headline='This is a test', pub_date=datetime(2005, 7, 27))
|
>>> a = Article(headline='This is a test', pub_date=datetime(2005, 7, 27))
|
||||||
>>> a.save()
|
>>> a.save()
|
||||||
|
|
||||||
# Create a few Writers.
|
# Create a few Writers.
|
||||||
>>> w1 = writers.Writer(reporter=r1, article=a, position='Main writer')
|
>>> w1 = Writer(reporter=r1, article=a, position='Main writer')
|
||||||
>>> w1.save()
|
>>> w1.save()
|
||||||
>>> w2 = writers.Writer(reporter=r2, article=a, position='Contributor')
|
>>> w2 = Writer(reporter=r2, article=a, position='Contributor')
|
||||||
>>> w2.save()
|
>>> w2.save()
|
||||||
|
|
||||||
# Play around with the API.
|
# Play around with the API.
|
||||||
|
@ -37,23 +37,23 @@ class Article(meta.Model):
|
|||||||
API_TESTS = """
|
API_TESTS = """
|
||||||
>>> from datetime import datetime
|
>>> from datetime import datetime
|
||||||
|
|
||||||
>>> c1 = categories.Category(name='Sports')
|
>>> c1 = Category(name='Sports')
|
||||||
>>> c1.save()
|
>>> c1.save()
|
||||||
>>> c2 = categories.Category(name='News')
|
>>> c2 = Category(name='News')
|
||||||
>>> c2.save()
|
>>> c2.save()
|
||||||
>>> c3 = categories.Category(name='Crime')
|
>>> c3 = Category(name='Crime')
|
||||||
>>> c3.save()
|
>>> c3.save()
|
||||||
>>> c4 = categories.Category(name='Life')
|
>>> c4 = Category(name='Life')
|
||||||
>>> c4.save()
|
>>> c4.save()
|
||||||
|
|
||||||
>>> a1 = articles.Article(headline='Area man steals', pub_date=datetime(2005, 11, 27))
|
>>> a1 = Article(headline='Area man steals', pub_date=datetime(2005, 11, 27))
|
||||||
>>> a1.save()
|
>>> a1.save()
|
||||||
>>> a1.set_primary_categories([c2.id, c3.id])
|
>>> a1.set_primary_categories([c2.id, c3.id])
|
||||||
True
|
True
|
||||||
>>> a1.set_secondary_categories([c4.id])
|
>>> a1.set_secondary_categories([c4.id])
|
||||||
True
|
True
|
||||||
|
|
||||||
>>> a2 = articles.Article(headline='Area man runs', pub_date=datetime(2005, 11, 28))
|
>>> a2 = Article(headline='Area man runs', pub_date=datetime(2005, 11, 28))
|
||||||
>>> a2.save()
|
>>> a2.save()
|
||||||
>>> a2.set_primary_categories([c1.id, c2.id])
|
>>> a2.set_primary_categories([c1.id, c2.id])
|
||||||
True
|
True
|
||||||
|
@ -23,9 +23,9 @@ class Category(meta.Model):
|
|||||||
|
|
||||||
API_TESTS = """
|
API_TESTS = """
|
||||||
# Create a few Category objects.
|
# Create a few Category objects.
|
||||||
>>> r = categories.Category(id=None, name='Root category', parent=None)
|
>>> r = Category(id=None, name='Root category', parent=None)
|
||||||
>>> r.save()
|
>>> r.save()
|
||||||
>>> c = categories.Category(id=None, name='Child category', parent=r)
|
>>> c = Category(id=None, name='Child category', parent=r)
|
||||||
>>> c.save()
|
>>> c.save()
|
||||||
|
|
||||||
>>> r.get_child_list()
|
>>> r.get_child_list()
|
||||||
@ -35,7 +35,7 @@ Child category
|
|||||||
>>> r.get_parent()
|
>>> r.get_parent()
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
CategoryDoesNotExist
|
DoesNotExist
|
||||||
|
|
||||||
>>> c.get_child_list()
|
>>> c.get_child_list()
|
||||||
[]
|
[]
|
||||||
|
@ -19,13 +19,13 @@ class Person(meta.Model):
|
|||||||
|
|
||||||
API_TESTS = """
|
API_TESTS = """
|
||||||
# Create two Person objects -- the mom and dad in our family.
|
# Create two Person objects -- the mom and dad in our family.
|
||||||
>>> dad = persons.Person(full_name='John Smith Senior', mother=None, father=None)
|
>>> dad = Person(full_name='John Smith Senior', mother=None, father=None)
|
||||||
>>> dad.save()
|
>>> dad.save()
|
||||||
>>> mom = persons.Person(full_name='Jane Smith', mother=None, father=None)
|
>>> mom = Person(full_name='Jane Smith', mother=None, father=None)
|
||||||
>>> mom.save()
|
>>> mom.save()
|
||||||
|
|
||||||
# Give mom and dad a kid.
|
# Give mom and dad a kid.
|
||||||
>>> kid = persons.Person(full_name='John Smith Junior', mother=mom, father=dad)
|
>>> kid = Person(full_name='John Smith Junior', mother=mom, father=dad)
|
||||||
>>> kid.save()
|
>>> kid.save()
|
||||||
|
|
||||||
>>> kid.get_mother()
|
>>> kid.get_mother()
|
||||||
|
@ -24,13 +24,13 @@ class Article(meta.Model):
|
|||||||
|
|
||||||
API_TESTS = """
|
API_TESTS = """
|
||||||
# Create a couple of Publications.
|
# Create a couple of Publications.
|
||||||
>>> p1 = publications.Publication(id=None, title='The Python Journal')
|
>>> p1 = Publication(id=None, title='The Python Journal')
|
||||||
>>> p1.save()
|
>>> p1.save()
|
||||||
>>> p2 = publications.Publication(id=None, title='Science News')
|
>>> p2 = Publication(id=None, title='Science News')
|
||||||
>>> p2.save()
|
>>> p2.save()
|
||||||
|
|
||||||
# Create an Article.
|
# Create an Article.
|
||||||
>>> a1 = articles.Article(id=None, headline='Django lets you build Web apps easily')
|
>>> a1 = Article(id=None, headline='Django lets you build Web apps easily')
|
||||||
>>> a1.save()
|
>>> a1.save()
|
||||||
|
|
||||||
# Associate the Article with one Publication. set_publications() returns a
|
# Associate the Article with one Publication. set_publications() returns a
|
||||||
@ -44,7 +44,7 @@ True
|
|||||||
False
|
False
|
||||||
|
|
||||||
# Create another Article, and set it to appear in both Publications.
|
# Create another Article, and set it to appear in both Publications.
|
||||||
>>> a2 = articles.Article(id=None, headline='NASA uses Python')
|
>>> a2 = Article(id=None, headline='NASA uses Python')
|
||||||
>>> a2.save()
|
>>> a2.save()
|
||||||
>>> a2.set_publications([p1.id, p2.id])
|
>>> a2.set_publications([p1.id, p2.id])
|
||||||
True
|
True
|
||||||
|
@ -24,12 +24,12 @@ class Article(meta.Model):
|
|||||||
|
|
||||||
API_TESTS = """
|
API_TESTS = """
|
||||||
# Create a Reporter.
|
# Create a Reporter.
|
||||||
>>> r = reporters.Reporter(first_name='John', last_name='Smith', email='john@example.com')
|
>>> r = Reporter(first_name='John', last_name='Smith', email='john@example.com')
|
||||||
>>> r.save()
|
>>> r.save()
|
||||||
|
|
||||||
# Create an Article.
|
# Create an Article.
|
||||||
>>> from datetime import datetime
|
>>> from datetime import datetime
|
||||||
>>> a = articles.Article(id=None, headline="This is a test", pub_date=datetime(2005, 7, 27), reporter=r)
|
>>> a = Article(id=None, headline="This is a test", pub_date=datetime(2005, 7, 27), reporter=r)
|
||||||
>>> a.save()
|
>>> a.save()
|
||||||
|
|
||||||
>>> a.reporter_id
|
>>> a.reporter_id
|
||||||
@ -64,26 +64,26 @@ This is a test
|
|||||||
# Use double underscores to separate relationships.
|
# Use double underscores to separate relationships.
|
||||||
# This works as many levels deep as you want. There's no limit.
|
# This works as many levels deep as you want. There's no limit.
|
||||||
# Find all Articles for any Reporter whose first name is "John".
|
# Find all Articles for any Reporter whose first name is "John".
|
||||||
>>> articles.get_list(reporter__first_name__exact='John', order_by=['pub_date'])
|
>>> Article.objects.get_list(reporter__first_name__exact='John', order_by=['pub_date'])
|
||||||
[This is a test, John's second story]
|
[This is a test, John's second story]
|
||||||
|
|
||||||
# Find all Articles for the Reporter whose ID is 1.
|
# Find all Articles for the Reporter whose ID is 1.
|
||||||
>>> articles.get_list(reporter__id__exact=1, order_by=['pub_date'])
|
>>> Article.objects.get_list(reporter__id__exact=1, order_by=['pub_date'])
|
||||||
[This is a test, John's second story]
|
[This is a test, John's second story]
|
||||||
|
|
||||||
# Note you need two underscores between "reporter" and "id" -- not one.
|
# Note you need two underscores between "reporter" and "id" -- not one.
|
||||||
>>> articles.get_list(reporter_id__exact=1)
|
>>> Article.objects.get_list(reporter_id__exact=1)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
TypeError: got unexpected keyword argument 'reporter_id__exact'
|
TypeError: got unexpected keyword argument 'reporter_id__exact'
|
||||||
|
|
||||||
# "pk" shortcut syntax works in a related context, too.
|
# "pk" shortcut syntax works in a related context, too.
|
||||||
>>> articles.get_list(reporter__pk=1, order_by=['pub_date'])
|
>>> Article.objects.get_list(reporter__pk=1, order_by=['pub_date'])
|
||||||
[This is a test, John's second story]
|
[This is a test, John's second story]
|
||||||
|
|
||||||
# You can also instantiate an Article by passing
|
# You can also instantiate an Article by passing
|
||||||
# the Reporter's ID instead of a Reporter object.
|
# the Reporter's ID instead of a Reporter object.
|
||||||
>>> a3 = articles.Article(id=None, headline="This is a test", pub_date=datetime(2005, 7, 27), reporter_id=r.id)
|
>>> a3 = Article(id=None, headline="This is a test", pub_date=datetime(2005, 7, 27), reporter_id=r.id)
|
||||||
>>> a3.save()
|
>>> a3.save()
|
||||||
>>> a3.reporter_id
|
>>> a3.reporter_id
|
||||||
1
|
1
|
||||||
@ -91,7 +91,7 @@ TypeError: got unexpected keyword argument 'reporter_id__exact'
|
|||||||
John Smith
|
John Smith
|
||||||
|
|
||||||
# Similarly, the reporter ID can be a string.
|
# Similarly, the reporter ID can be a string.
|
||||||
>>> a4 = articles.Article(id=None, headline="This is a test", pub_date=datetime(2005, 7, 27), reporter_id="1")
|
>>> a4 = Article(id=None, headline="This is a test", pub_date=datetime(2005, 7, 27), reporter_id="1")
|
||||||
>>> a4.save()
|
>>> a4.save()
|
||||||
>>> a4.get_reporter()
|
>>> a4.get_reporter()
|
||||||
John Smith
|
John Smith
|
||||||
|
@ -22,11 +22,11 @@ class Article(meta.Model):
|
|||||||
|
|
||||||
API_TESTS = """
|
API_TESTS = """
|
||||||
# Create a Reporter.
|
# Create a Reporter.
|
||||||
>>> r = reporters.Reporter(name='John Smith')
|
>>> r = Reporter(name='John Smith')
|
||||||
>>> r.save()
|
>>> r.save()
|
||||||
|
|
||||||
# Create an Article.
|
# Create an Article.
|
||||||
>>> a = articles.Article(headline="First", reporter=r)
|
>>> a = Article(headline="First", reporter=r)
|
||||||
>>> a.save()
|
>>> a.save()
|
||||||
|
|
||||||
>>> a.reporter_id
|
>>> a.reporter_id
|
||||||
@ -54,14 +54,14 @@ First
|
|||||||
2
|
2
|
||||||
|
|
||||||
# Create an Article with no Reporter by passing "reporter=None".
|
# Create an Article with no Reporter by passing "reporter=None".
|
||||||
>>> a3 = articles.Article(headline="Third", reporter=None)
|
>>> a3 = Article(headline="Third", reporter=None)
|
||||||
>>> a3.save()
|
>>> a3.save()
|
||||||
>>> a3.id
|
>>> a3.id
|
||||||
3
|
3
|
||||||
>>> a3.reporter_id
|
>>> a3.reporter_id
|
||||||
>>> print a3.reporter_id
|
>>> print a3.reporter_id
|
||||||
None
|
None
|
||||||
>>> a3 = articles.get_object(pk=3)
|
>>> a3 = Article.objects.get_object(pk=3)
|
||||||
>>> print a3.reporter_id
|
>>> print a3.reporter_id
|
||||||
None
|
None
|
||||||
|
|
||||||
@ -70,9 +70,9 @@ None
|
|||||||
>>> a3.get_reporter()
|
>>> a3.get_reporter()
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ReporterDoesNotExist
|
DoesNotExist
|
||||||
|
|
||||||
# To retrieve the articles with no reporters set, use "reporter__isnull=True".
|
# To retrieve the articles with no reporters set, use "reporter__isnull=True".
|
||||||
>>> articles.get_list(reporter__isnull=True)
|
>>> Article.objects.get_list(reporter__isnull=True)
|
||||||
[Third]
|
[Third]
|
||||||
"""
|
"""
|
||||||
|
@ -32,13 +32,13 @@ class Waiter(meta.Model):
|
|||||||
|
|
||||||
API_TESTS = """
|
API_TESTS = """
|
||||||
# Create a couple of Places.
|
# Create a couple of Places.
|
||||||
>>> p1 = places.Place(name='Demon Dogs', address='944 W. Fullerton')
|
>>> p1 = Place(name='Demon Dogs', address='944 W. Fullerton')
|
||||||
>>> p1.save()
|
>>> p1.save()
|
||||||
>>> p2 = places.Place(name='Ace Hardware', address='1013 N. Ashland')
|
>>> p2 = Place(name='Ace Hardware', address='1013 N. Ashland')
|
||||||
>>> p2.save()
|
>>> p2.save()
|
||||||
|
|
||||||
# Create a Restaurant. Pass the ID of the "parent" object as this object's ID.
|
# Create a Restaurant. Pass the ID of the "parent" object as this object's ID.
|
||||||
>>> r = restaurants.Restaurant(place=p1, serves_hot_dogs=True, serves_pizza=False)
|
>>> r = Restaurant(place=p1, serves_hot_dogs=True, serves_pizza=False)
|
||||||
>>> r.save()
|
>>> r.save()
|
||||||
|
|
||||||
# A Restaurant can access its place.
|
# A Restaurant can access its place.
|
||||||
@ -53,20 +53,20 @@ Demon Dogs the restaurant
|
|||||||
>>> p2.get_restaurant()
|
>>> p2.get_restaurant()
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
RestaurantDoesNotExist: Restaurant does not exist for {'place__id__exact': ...}
|
DoesNotExist: Restaurant does not exist for {'place__id__exact': ...}
|
||||||
|
|
||||||
# restaurants.get_list() just returns the Restaurants, not the Places.
|
# Restaurant.objects.get_list() just returns the Restaurants, not the Places.
|
||||||
>>> restaurants.get_list()
|
>>> Restaurant.objects.get_list()
|
||||||
[Demon Dogs the restaurant]
|
[Demon Dogs the restaurant]
|
||||||
|
|
||||||
# places.get_list() returns all Places, regardless of whether they have
|
# Place.objects.get_list() returns all Places, regardless of whether they have
|
||||||
# Restaurants.
|
# Restaurants.
|
||||||
>>> places.get_list(order_by=['name'])
|
>>> Place.objects.get_list(order_by=['name'])
|
||||||
[Ace Hardware the place, Demon Dogs the place]
|
[Ace Hardware the place, Demon Dogs the place]
|
||||||
|
|
||||||
>>> restaurants.get_object(place__id__exact=1)
|
>>> Restaurant.objects.get_object(place__id__exact=1)
|
||||||
Demon Dogs the restaurant
|
Demon Dogs the restaurant
|
||||||
>>> restaurants.get_object(pk=1)
|
>>> Restaurant.objects.get_object(pk=1)
|
||||||
Demon Dogs the restaurant
|
Demon Dogs the restaurant
|
||||||
|
|
||||||
# Add a Waiter to the Restaurant.
|
# Add a Waiter to the Restaurant.
|
||||||
@ -75,6 +75,6 @@ Demon Dogs the restaurant
|
|||||||
>>> w
|
>>> w
|
||||||
Joe the waiter at Demon Dogs the restaurant
|
Joe the waiter at Demon Dogs the restaurant
|
||||||
|
|
||||||
>>> r = restaurants.get_object(pk=1)
|
>>> r = Restaurant.objects.get_object(pk=1)
|
||||||
>>> r.delete()
|
>>> r.delete()
|
||||||
"""
|
"""
|
||||||
|
@ -21,37 +21,37 @@ API_TESTS = """
|
|||||||
>>> from datetime import datetime
|
>>> from datetime import datetime
|
||||||
>>> from django.core.meta import Q
|
>>> from django.core.meta import Q
|
||||||
|
|
||||||
>>> a1 = articles.Article(headline='Hello', pub_date=datetime(2005, 11, 27))
|
>>> a1 = Article(headline='Hello', pub_date=datetime(2005, 11, 27))
|
||||||
>>> a1.save()
|
>>> a1.save()
|
||||||
|
|
||||||
>>> a2 = articles.Article(headline='Goodbye', pub_date=datetime(2005, 11, 28))
|
>>> a2 = Article(headline='Goodbye', pub_date=datetime(2005, 11, 28))
|
||||||
>>> a2.save()
|
>>> a2.save()
|
||||||
|
|
||||||
>>> a3 = articles.Article(headline='Hello and goodbye', pub_date=datetime(2005, 11, 29))
|
>>> a3 = Article(headline='Hello and goodbye', pub_date=datetime(2005, 11, 29))
|
||||||
>>> a3.save()
|
>>> a3.save()
|
||||||
|
|
||||||
>>> articles.get_list(complex=(Q(headline__startswith='Hello') | Q(headline__startswith='Goodbye')))
|
>>> Article.objects.get_list(complex=(Q(headline__startswith='Hello') | Q(headline__startswith='Goodbye')))
|
||||||
[Hello, Goodbye, Hello and goodbye]
|
[Hello, Goodbye, Hello and goodbye]
|
||||||
|
|
||||||
>>> articles.get_list(complex=(Q(headline__startswith='Hello') & Q(headline__startswith='Goodbye')))
|
>>> Article.objects.get_list(complex=(Q(headline__startswith='Hello') & Q(headline__startswith='Goodbye')))
|
||||||
[]
|
[]
|
||||||
|
|
||||||
>>> articles.get_list(complex=(Q(headline__startswith='Hello') & Q(headline__contains='bye')))
|
>>> Article.objects.get_list(complex=(Q(headline__startswith='Hello') & Q(headline__contains='bye')))
|
||||||
[Hello and goodbye]
|
[Hello and goodbye]
|
||||||
|
|
||||||
>>> articles.get_list(headline__startswith='Hello', complex=Q(headline__contains='bye'))
|
>>> Article.objects.get_list(headline__startswith='Hello', complex=Q(headline__contains='bye'))
|
||||||
[Hello and goodbye]
|
[Hello and goodbye]
|
||||||
|
|
||||||
>>> articles.get_list(complex=(Q(headline__contains='Hello') | Q(headline__contains='bye')))
|
>>> Article.objects.get_list(complex=(Q(headline__contains='Hello') | Q(headline__contains='bye')))
|
||||||
[Hello, Goodbye, Hello and goodbye]
|
[Hello, Goodbye, Hello and goodbye]
|
||||||
|
|
||||||
>>> articles.get_list(complex=(Q(headline__iexact='Hello') | Q(headline__contains='ood')))
|
>>> Article.objects.get_list(complex=(Q(headline__iexact='Hello') | Q(headline__contains='ood')))
|
||||||
[Hello, Goodbye, Hello and goodbye]
|
[Hello, Goodbye, Hello and goodbye]
|
||||||
|
|
||||||
>>> articles.get_list(complex=(Q(pk=1) | Q(pk=2)))
|
>>> Article.objects.get_list(complex=(Q(pk=1) | Q(pk=2)))
|
||||||
[Hello, Goodbye]
|
[Hello, Goodbye]
|
||||||
|
|
||||||
>>> articles.get_list(complex=(Q(pk=1) | Q(pk=2) | Q(pk=3)))
|
>>> Article.objects.get_list(complex=(Q(pk=1) | Q(pk=2) | Q(pk=3)))
|
||||||
[Hello, Goodbye, Hello and goodbye]
|
[Hello, Goodbye, Hello and goodbye]
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
@ -27,37 +27,37 @@ class Article(meta.Model):
|
|||||||
API_TESTS = """
|
API_TESTS = """
|
||||||
# Create a couple of Articles.
|
# Create a couple of Articles.
|
||||||
>>> from datetime import datetime
|
>>> from datetime import datetime
|
||||||
>>> a1 = articles.Article(headline='Article 1', pub_date=datetime(2005, 7, 26))
|
>>> a1 = Article(headline='Article 1', pub_date=datetime(2005, 7, 26))
|
||||||
>>> a1.save()
|
>>> a1.save()
|
||||||
>>> a2 = articles.Article(headline='Article 2', pub_date=datetime(2005, 7, 27))
|
>>> a2 = Article(headline='Article 2', pub_date=datetime(2005, 7, 27))
|
||||||
>>> a2.save()
|
>>> a2.save()
|
||||||
>>> a3 = articles.Article(headline='Article 3', pub_date=datetime(2005, 7, 27))
|
>>> a3 = Article(headline='Article 3', pub_date=datetime(2005, 7, 27))
|
||||||
>>> a3.save()
|
>>> a3.save()
|
||||||
>>> a4 = articles.Article(headline='Article 4', pub_date=datetime(2005, 7, 28))
|
>>> a4 = Article(headline='Article 4', pub_date=datetime(2005, 7, 28))
|
||||||
>>> a4.save()
|
>>> a4.save()
|
||||||
|
|
||||||
# By default, articles.get_list() orders by pub_date descending, then
|
# By default, articles.get_list() orders by pub_date descending, then
|
||||||
# headline ascending.
|
# headline ascending.
|
||||||
>>> articles.get_list()
|
>>> Article.objects.get_list()
|
||||||
[Article 4, Article 2, Article 3, Article 1]
|
[Article 4, Article 2, Article 3, Article 1]
|
||||||
|
|
||||||
# Override ordering with order_by, which is in the same format as the ordering
|
# Override ordering with order_by, which is in the same format as the ordering
|
||||||
# attribute in models.
|
# attribute in models.
|
||||||
>>> articles.get_list(order_by=['headline'])
|
>>> Article.objects.get_list(order_by=['headline'])
|
||||||
[Article 1, Article 2, Article 3, Article 4]
|
[Article 1, Article 2, Article 3, Article 4]
|
||||||
>>> articles.get_list(order_by=['pub_date', '-headline'])
|
>>> Article.objects.get_list(order_by=['pub_date', '-headline'])
|
||||||
[Article 1, Article 3, Article 2, Article 4]
|
[Article 1, Article 3, Article 2, Article 4]
|
||||||
|
|
||||||
# Use the "limit" parameter to limit the results.
|
# Use the "limit" parameter to limit the results.
|
||||||
>>> articles.get_list(order_by=['headline'], limit=2)
|
>>> Article.objects.get_list(order_by=['headline'], limit=2)
|
||||||
[Article 1, Article 2]
|
[Article 1, Article 2]
|
||||||
|
|
||||||
# Use the "offset" parameter with "limit" to offset the result list.
|
# Use the "offset" parameter with "limit" to offset the result list.
|
||||||
>>> articles.get_list(order_by=['headline'], offset=1, limit=2)
|
>>> Article.objects.get_list(order_by=['headline'], offset=1, limit=2)
|
||||||
[Article 2, Article 3]
|
[Article 2, Article 3]
|
||||||
|
|
||||||
# Use '?' to order randomly. (We're using [...] in the output to indicate we
|
# Use '?' to order randomly. (We're using [...] in the output to indicate we
|
||||||
# don't know what order the output will be in.
|
# don't know what order the output will be in.
|
||||||
>>> articles.get_list(order_by=['?'])
|
>>> Article.objects.get_list(order_by=['?'])
|
||||||
[...]
|
[...]
|
||||||
"""
|
"""
|
||||||
|
@ -20,7 +20,7 @@ class Article(meta.Model):
|
|||||||
API_TESTS = """
|
API_TESTS = """
|
||||||
# Create an Article.
|
# Create an Article.
|
||||||
>>> from datetime import datetime
|
>>> from datetime import datetime
|
||||||
>>> a = articles.Article(headline='Area man programs in Python', pub_date=datetime(2005, 7, 28))
|
>>> a = Article(headline='Area man programs in Python', pub_date=datetime(2005, 7, 28))
|
||||||
>>> a.save()
|
>>> a.save()
|
||||||
|
|
||||||
>>> repr(a)
|
>>> repr(a)
|
||||||
|
@ -25,23 +25,23 @@ class Thing(meta.Model):
|
|||||||
return self.when
|
return self.when
|
||||||
|
|
||||||
API_TESTS = """
|
API_TESTS = """
|
||||||
>>> t = things.Thing(when='a', join='b', like='c', drop='d', alter='e', having='f', where='g', has_hyphen='h')
|
>>> t = Thing(when='a', join='b', like='c', drop='d', alter='e', having='f', where='g', has_hyphen='h')
|
||||||
>>> t.save()
|
>>> t.save()
|
||||||
>>> print t.when
|
>>> print t.when
|
||||||
a
|
a
|
||||||
|
|
||||||
>>> u = things.Thing(when='h', join='i', like='j', drop='k', alter='l', having='m', where='n')
|
>>> u = Thing(when='h', join='i', like='j', drop='k', alter='l', having='m', where='n')
|
||||||
>>> u.save()
|
>>> u.save()
|
||||||
>>> print u.when
|
>>> print u.when
|
||||||
h
|
h
|
||||||
|
|
||||||
>>> things.get_list(order_by=['when'])
|
>>> Thing.objects.get_list(order_by=['when'])
|
||||||
[a, h]
|
[a, h]
|
||||||
>>> v = things.get_object(pk='a')
|
>>> v = Thing.objects.get_object(pk='a')
|
||||||
>>> print v.join
|
>>> print v.join
|
||||||
b
|
b
|
||||||
>>> print v.where
|
>>> print v.where
|
||||||
g
|
g
|
||||||
>>> things.get_list(order_by=['select.when'])
|
>>> Thing.objects.get_list(order_by=['select.when'])
|
||||||
[a, h]
|
[a, h]
|
||||||
"""
|
"""
|
||||||
|
@ -32,18 +32,18 @@ class Person(meta.Model):
|
|||||||
print "After deletion"
|
print "After deletion"
|
||||||
|
|
||||||
API_TESTS = """
|
API_TESTS = """
|
||||||
>>> p1 = persons.Person(first_name='John', last_name='Smith')
|
>>> p1 = Person(first_name='John', last_name='Smith')
|
||||||
>>> p1.save()
|
>>> p1.save()
|
||||||
Before save
|
Before save
|
||||||
After save
|
After save
|
||||||
|
|
||||||
>>> persons.get_list()
|
>>> Person.objects.get_list()
|
||||||
[John Smith]
|
[John Smith]
|
||||||
|
|
||||||
>>> p1.delete()
|
>>> p1.delete()
|
||||||
Before deletion
|
Before deletion
|
||||||
After deletion
|
After deletion
|
||||||
|
|
||||||
>>> persons.get_list()
|
>>> Person.objects.get_list()
|
||||||
[]
|
[]
|
||||||
"""
|
"""
|
||||||
|
@ -63,16 +63,16 @@ class NoModuleNameSecond(Article):
|
|||||||
|
|
||||||
API_TESTS = """
|
API_TESTS = """
|
||||||
# No data is in the system yet.
|
# No data is in the system yet.
|
||||||
>>> subarticles1.get_list()
|
>>> ArticleWithSection.objects.get_list()
|
||||||
[]
|
[]
|
||||||
>>> subarticles2.get_list()
|
>>> ArticleWithoutPubDate.objects.get_list()
|
||||||
[]
|
[]
|
||||||
>>> subarticles3.get_list()
|
>>> ArticleWithFieldOverride.objects.get_list()
|
||||||
[]
|
[]
|
||||||
|
|
||||||
# Create an ArticleWithSection.
|
# Create an ArticleWithSection.
|
||||||
>>> from datetime import date, datetime
|
>>> from datetime import date, datetime
|
||||||
>>> a1 = subarticles1.ArticleWithSection(headline='First', pub_date=datetime(2005, 8, 22), section='News')
|
>>> a1 = ArticleWithSection(headline='First', pub_date=datetime(2005, 8, 22), section='News')
|
||||||
>>> a1.save()
|
>>> a1.save()
|
||||||
>>> a1
|
>>> a1
|
||||||
<ArticleWithSection object>
|
<ArticleWithSection object>
|
||||||
@ -84,7 +84,7 @@ API_TESTS = """
|
|||||||
datetime.datetime(2005, 8, 22, 0, 0)
|
datetime.datetime(2005, 8, 22, 0, 0)
|
||||||
|
|
||||||
# Retrieve it again, to prove the fields have been saved.
|
# Retrieve it again, to prove the fields have been saved.
|
||||||
>>> a1 = subarticles1.get_object(pk=1)
|
>>> a1 = ArticleWithSection.objects.get_object(pk=1)
|
||||||
>>> a1.headline
|
>>> a1.headline
|
||||||
'First'
|
'First'
|
||||||
>>> a1.pub_date
|
>>> a1.pub_date
|
||||||
@ -93,7 +93,7 @@ datetime.datetime(2005, 8, 22, 0, 0)
|
|||||||
'News'
|
'News'
|
||||||
|
|
||||||
# Create an ArticleWithoutPubDate.
|
# Create an ArticleWithoutPubDate.
|
||||||
>>> a2 = subarticles2.ArticleWithoutPubDate(headline='Second')
|
>>> a2 = ArticleWithoutPubDate(headline='Second')
|
||||||
>>> a2.save()
|
>>> a2.save()
|
||||||
>>> a2
|
>>> a2
|
||||||
<ArticleWithoutPubDate object>
|
<ArticleWithoutPubDate object>
|
||||||
@ -105,7 +105,7 @@ Traceback (most recent call last):
|
|||||||
AttributeError: 'ArticleWithoutPubDate' object has no attribute 'pub_date'
|
AttributeError: 'ArticleWithoutPubDate' object has no attribute 'pub_date'
|
||||||
|
|
||||||
# Retrieve it again, to prove the fields have been saved.
|
# Retrieve it again, to prove the fields have been saved.
|
||||||
>>> a2 = subarticles2.get_object(pk=1)
|
>>> a2 = ArticleWithoutPubDate.objects.get_object(pk=1)
|
||||||
>>> a2.headline
|
>>> a2.headline
|
||||||
'Second'
|
'Second'
|
||||||
>>> a2.pub_date
|
>>> a2.pub_date
|
||||||
@ -114,7 +114,7 @@ Traceback (most recent call last):
|
|||||||
AttributeError: 'ArticleWithoutPubDate' object has no attribute 'pub_date'
|
AttributeError: 'ArticleWithoutPubDate' object has no attribute 'pub_date'
|
||||||
|
|
||||||
# Create an ArticleWithFieldOverride.
|
# Create an ArticleWithFieldOverride.
|
||||||
>>> a3 = subarticles3.ArticleWithFieldOverride(headline='Third', pub_date=date(2005, 8, 22))
|
>>> a3 = ArticleWithFieldOverride(headline='Third', pub_date=date(2005, 8, 22))
|
||||||
>>> a3.save()
|
>>> a3.save()
|
||||||
>>> a3
|
>>> a3
|
||||||
<ArticleWithFieldOverride object>
|
<ArticleWithFieldOverride object>
|
||||||
@ -124,14 +124,14 @@ AttributeError: 'ArticleWithoutPubDate' object has no attribute 'pub_date'
|
|||||||
datetime.date(2005, 8, 22)
|
datetime.date(2005, 8, 22)
|
||||||
|
|
||||||
# Retrieve it again, to prove the fields have been saved.
|
# Retrieve it again, to prove the fields have been saved.
|
||||||
>>> a3 = subarticles3.get_object(pk=1)
|
>>> a3 = ArticleWithFieldOverride.objects.get_object(pk=1)
|
||||||
>>> a3.headline
|
>>> a3.headline
|
||||||
'Third'
|
'Third'
|
||||||
>>> a3.pub_date
|
>>> a3.pub_date
|
||||||
datetime.date(2005, 8, 22)
|
datetime.date(2005, 8, 22)
|
||||||
|
|
||||||
# Create an ArticleWithManyChanges.
|
# Create an ArticleWithManyChanges.
|
||||||
>>> a4 = subarticles4.ArticleWithManyChanges(headline='Fourth', section='Arts',
|
>>> a4 = ArticleWithManyChanges(headline='Fourth', section='Arts',
|
||||||
... is_popular=True, pub_date=date(2005, 8, 22))
|
... is_popular=True, pub_date=date(2005, 8, 22))
|
||||||
>>> a4.save()
|
>>> a4.save()
|
||||||
|
|
||||||
@ -140,7 +140,7 @@ datetime.date(2005, 8, 22)
|
|||||||
Fourth
|
Fourth
|
||||||
|
|
||||||
# Retrieve it again, to prove the fields have been saved.
|
# Retrieve it again, to prove the fields have been saved.
|
||||||
>>> a4 = subarticles4.get_object(pk=1)
|
>>> a4 = ArticleWithManyChanges.objects.get_object(pk=1)
|
||||||
>>> a4.headline
|
>>> a4.headline
|
||||||
'Fourth'
|
'Fourth'
|
||||||
>>> a4.section
|
>>> a4.section
|
||||||
@ -151,26 +151,26 @@ True
|
|||||||
datetime.date(2005, 8, 22)
|
datetime.date(2005, 8, 22)
|
||||||
|
|
||||||
# Test get_list().
|
# Test get_list().
|
||||||
>>> subarticles1.get_list()
|
>>> ArticleWithSection.objects.get_list()
|
||||||
[<ArticleWithSection object>]
|
[<ArticleWithSection object>]
|
||||||
>>> subarticles2.get_list()
|
>>> ArticleWithoutPubDate.objects.get_list()
|
||||||
[<ArticleWithoutPubDate object>]
|
[<ArticleWithoutPubDate object>]
|
||||||
>>> subarticles3.get_list()
|
>>> ArticleWithFieldOverride.objects.get_list()
|
||||||
[<ArticleWithFieldOverride object>]
|
[<ArticleWithFieldOverride object>]
|
||||||
>>> subarticles4.get_list()
|
>>> ArticleWithManyChanges.objects.get_list()
|
||||||
[Fourth]
|
[Fourth]
|
||||||
|
|
||||||
# Create a couple of ArticleWithChangedMeta objects.
|
# Create a couple of ArticleWithChangedMeta objects.
|
||||||
>>> a5 = subarticles5.ArticleWithChangedMeta(headline='A', pub_date=datetime(2005, 3, 1))
|
>>> a5 = ArticleWithChangedMeta(headline='A', pub_date=datetime(2005, 3, 1))
|
||||||
>>> a5.save()
|
>>> a5.save()
|
||||||
>>> a6 = subarticles5.ArticleWithChangedMeta(headline='B', pub_date=datetime(2005, 4, 1))
|
>>> a6 = ArticleWithChangedMeta(headline='B', pub_date=datetime(2005, 4, 1))
|
||||||
>>> a6.save()
|
>>> a6.save()
|
||||||
>>> a7 = subarticles5.ArticleWithChangedMeta(headline='C', pub_date=datetime(2005, 5, 1))
|
>>> a7 = ArticleWithChangedMeta(headline='C', pub_date=datetime(2005, 5, 1))
|
||||||
>>> a7.save()
|
>>> a7.save()
|
||||||
|
|
||||||
# Ordering has been overridden, so objects are ordered
|
# Ordering has been overridden, so objects are ordered
|
||||||
# by headline ASC instead of pub_date DESC.
|
# by headline ASC instead of pub_date DESC.
|
||||||
>>> subarticles5.get_list()
|
>>> ArticleWithChangedMeta.objects.get_list()
|
||||||
[A, B, C]
|
[A, B, C]
|
||||||
|
|
||||||
>>> nomodulenamefirsts.get_list()
|
>>> nomodulenamefirsts.get_list()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user