1
0
mirror of https://github.com/django/django.git synced 2025-05-06 23:16: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:
Adrian Holovaty 2005-12-11 22:10:02 +00:00
parent 69b82e1ca1
commit c1be782510
21 changed files with 186 additions and 186 deletions

View File

@ -132,7 +132,7 @@ class TestRunner:
# Run the API tests.
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)
# Manually set verbose=False, because "-v" command-line parameter
# has side effects on doctest TestRunner class.

View File

@ -12,12 +12,12 @@ class Article(meta.Model):
API_TESTS = """
# No articles are in the system yet.
>>> articles.get_list()
>>> Article.objects.get_list()
[]
# Create an Article.
>>> 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))
# 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
# is represented by "<Article object>", because we haven't given the Article
# model a __repr__() method.
>>> articles.get_list()
>>> Article.objects.get_list()
[<Article object>]
# Django provides a rich database lookup API that's entirely driven by
# keyword arguments.
>>> articles.get_object(id__exact=1)
>>> Article.objects.get_object(id__exact=1)
<Article object>
>>> articles.get_object(headline__startswith='Area woman')
>>> Article.objects.get_object(headline__startswith='Area woman')
<Article object>
>>> articles.get_object(pub_date__year=2005)
>>> Article.objects.get_object(pub_date__year=2005)
<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>
>>> 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>
>>> articles.get_list(pub_date__year=2005)
>>> Article.objects.get_list(pub_date__year=2005)
[<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>]
# 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):
...
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):
...
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
# shortcut for primary-key exact lookups.
# The following is identical to articles.get_object(id__exact=1).
>>> articles.get_object(pk=1)
>>> Article.objects.get_object(pk=1)
<Article object>
# Model instances of the same type and same ID are considered equal.
>>> a = articles.get_object(pk=1)
>>> b = articles.get_object(pk=1)
>>> a = Article.objects.get_object(pk=1)
>>> b = Article.objects.get_object(pk=1)
>>> a == b
True
# You can initialize a model instance using positional arguments, which should
# 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.id
2L
@ -98,7 +98,7 @@ True
datetime.datetime(2005, 7, 29, 0, 0)
# ...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))
>>> a3.save()
>>> 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
# 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.headline
'Fourth article'
# 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):
...
TypeError: 'foo' is an invalid keyword argument for this function
# 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.id
5L
@ -130,21 +130,21 @@ TypeError: 'foo' is an invalid keyword argument for this function
'Article 6'
# 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.headline
'Default headline'
# For DateTimeFields, Django saves as much precision (in seconds) as you
# 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()
>>> articles.get_object(id__exact=7).pub_date
>>> Article.objects.get_object(id__exact=7).pub_date
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()
>>> 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)
>>> a8.id
8L
@ -160,13 +160,13 @@ datetime.datetime(2005, 7, 31, 12, 30, 45)
>>> a7 == a8
False
>>> a8 == articles.get_object(id__exact=8)
>>> a8 == Article.objects.get_object(id__exact=8)
True
>>> a7 != a8
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
>>> 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
"""
@ -177,9 +177,9 @@ building_docs = getattr(settings, 'BUILDING_DOCS', False)
if building_docs or settings.DATABASE_ENGINE == 'postgresql':
API_TESTS += """
# 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()
>>> 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)
"""
@ -187,18 +187,18 @@ 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.
>>> 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()
>>> 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)
"""
API_TESTS += """
# 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 = articles.get_object(pk=101)
>>> a101 = Article.objects.get_object(pk=101)
>>> a101.headline
'Article 101'
"""

View File

@ -17,27 +17,27 @@ class Person(meta.Model):
API_TESTS = """
# Create a Person.
>>> p = persons.Person(first_name='John', last_name='Smith')
>>> p = Person(first_name='John', last_name='Smith')
>>> p.save()
>>> p.id
1
>>> persons.get_list()
>>> Person.objects.get_list()
[John Smith]
>>> persons.get_list(first_name__exact='John')
>>> Person.objects.get_list(first_name__exact='John')
[John Smith]
>>> persons.get_object(first_name__exact='John')
>>> Person.objects.get_object(first_name__exact='John')
John Smith
>>> persons.get_list(firstname__exact='John')
>>> Person.objects.get_list(firstname__exact='John')
Traceback (most recent call last):
...
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
'John'
>>> p.last_name

View File

@ -53,9 +53,9 @@ class Article(meta.Model):
API_TESTS = """
# Create a couple of Articles.
>>> 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()
>>> 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()
# Test the custom methods.

View File

@ -28,35 +28,35 @@ class Business(meta.Model):
return self.name
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()
>>> employees.get_list()
>>> Employee.objects.get_list()
[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()
>>> employees.get_list()
>>> Employee.objects.get_list()
[Fran Bones, Dan Jones]
>>> employees.get_object(pk='ABC123')
>>> Employee.objects.get_object(pk='ABC123')
Dan Jones
>>> employees.get_object(pk='XYZ456')
>>> Employee.objects.get_object(pk='XYZ456')
Fran Bones
>>> employees.get_object(pk='foo')
>>> Employee.objects.get_object(pk='foo')
Traceback (most recent call last):
...
EmployeeDoesNotExist: Employee does not exist for {'pk': 'foo'}
# 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.save()
>>> employees.get_list(last_name__exact='Jones')
>>> Employee.objects.get_list(last_name__exact='Jones')
[Dan Jones, Fran Jones]
>>> employees.get_in_bulk(['ABC123', 'XYZ456'])
>>> Employee.objects.get_in_bulk(['ABC123', 'XYZ456'])
{'XYZ456': Fran Jones, 'ABC123': Dan Jones}
>>> b = businesses.Business(name='Sears')
>>> b = Business(name='Sears')
>>> b.save()
>>> b.set_employees([dan.employee_code, fran.employee_code])
True
@ -64,6 +64,6 @@ True
[Dan Jones, Fran Jones]
>>> fran.get_business_list()
[Sears]
>>> businesses.get_in_bulk(['Sears'])
>>> Business.objects.get_in_bulk(['Sears'])
{'Sears': Sears}
"""

View File

@ -21,23 +21,23 @@ class Article(meta.Model):
API_TESTS = """
# Because no Articles exist yet, get_latest() raises ArticleDoesNotExist.
>>> articles.get_latest()
>>> Article.objects.get_latest()
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.
>>> 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()
>>> 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()
>>> 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()
>>> 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()
# Get the latest Article.
>>> articles.get_latest()
>>> Article.objects.get_latest()
Article 4
"""

View File

@ -18,23 +18,23 @@ class Article(meta.Model):
API_TESTS = """
# Create a couple of Articles.
>>> 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()
>>> 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()
>>> 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()
>>> 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()
>>> 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()
>>> 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()
>>> 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()
# 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
Article 5
Article 6
@ -45,39 +45,39 @@ Article 7
Article 1
# 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
Article 4
# get_count() returns the number of objects matching search criteria.
>>> articles.get_count()
>>> Article.objects.get_count()
7L
>>> articles.get_count(pub_date__exact=datetime(2005, 7, 27))
>>> Article.objects.get_count(pub_date__exact=datetime(2005, 7, 27))
3L
>>> articles.get_count(headline__startswith='Blah blah')
>>> Article.objects.get_count(headline__startswith='Blah blah')
0L
# get_in_bulk() takes a list of IDs and returns a dictionary mapping IDs
# to objects.
>>> articles.get_in_bulk([1, 2])
>>> Article.objects.get_in_bulk([1, 2])
{1: Article 1, 2: Article 2}
>>> articles.get_in_bulk([3])
>>> Article.objects.get_in_bulk([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
# dictionaries instead of object instances -- and you can specify which fields
# 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'}]
>>> 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}]
>>> 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
# 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.sort()
... i
@ -104,7 +104,7 @@ Article 6
>>> a5.get_next_by_pub_date()
Traceback (most recent call last):
...
ArticleDoesNotExist: Article does not exist for ...
DoesNotExist: Article does not exist for ...
>>> a6.get_next_by_pub_date()
Article 5
>>> a7.get_next_by_pub_date()
@ -125,16 +125,16 @@ Article 1
# Underscores and percent signs have special meaning in the underlying
# 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()
>>> 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]
>>> articles.get_list(headline__startswith='Article_')
>>> Article.objects.get_list(headline__startswith='Article_')
[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()
>>> 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]
>>> articles.get_list(headline__startswith='Article%')
>>> Article.objects.get_list(headline__startswith='Article%')
[Article% with percent sign]
"""

View File

@ -36,20 +36,20 @@ class Writer(meta.Model):
API_TESTS = """
# Create a few Reporters.
>>> r1 = reporters.Reporter(first_name='John', last_name='Smith')
>>> r1 = Reporter(first_name='John', last_name='Smith')
>>> r1.save()
>>> r2 = reporters.Reporter(first_name='Jane', last_name='Doe')
>>> r2 = Reporter(first_name='Jane', last_name='Doe')
>>> r2.save()
# Create an Article.
>>> 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()
# 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()
>>> w2 = writers.Writer(reporter=r2, article=a, position='Contributor')
>>> w2 = Writer(reporter=r2, article=a, position='Contributor')
>>> w2.save()
# Play around with the API.

View File

@ -37,23 +37,23 @@ class Article(meta.Model):
API_TESTS = """
>>> from datetime import datetime
>>> c1 = categories.Category(name='Sports')
>>> c1 = Category(name='Sports')
>>> c1.save()
>>> c2 = categories.Category(name='News')
>>> c2 = Category(name='News')
>>> c2.save()
>>> c3 = categories.Category(name='Crime')
>>> c3 = Category(name='Crime')
>>> c3.save()
>>> c4 = categories.Category(name='Life')
>>> c4 = Category(name='Life')
>>> 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.set_primary_categories([c2.id, c3.id])
True
>>> a1.set_secondary_categories([c4.id])
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.set_primary_categories([c1.id, c2.id])
True

View File

@ -23,9 +23,9 @@ class Category(meta.Model):
API_TESTS = """
# 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()
>>> c = categories.Category(id=None, name='Child category', parent=r)
>>> c = Category(id=None, name='Child category', parent=r)
>>> c.save()
>>> r.get_child_list()
@ -35,7 +35,7 @@ Child category
>>> r.get_parent()
Traceback (most recent call last):
...
CategoryDoesNotExist
DoesNotExist
>>> c.get_child_list()
[]

View File

@ -19,13 +19,13 @@ class Person(meta.Model):
API_TESTS = """
# 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()
>>> mom = persons.Person(full_name='Jane Smith', mother=None, father=None)
>>> mom = Person(full_name='Jane Smith', mother=None, father=None)
>>> mom.save()
# 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.get_mother()

View File

@ -24,13 +24,13 @@ class Article(meta.Model):
API_TESTS = """
# Create a couple of Publications.
>>> p1 = publications.Publication(id=None, title='The Python Journal')
>>> p1 = Publication(id=None, title='The Python Journal')
>>> p1.save()
>>> p2 = publications.Publication(id=None, title='Science News')
>>> p2 = Publication(id=None, title='Science News')
>>> p2.save()
# 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()
# Associate the Article with one Publication. set_publications() returns a
@ -44,7 +44,7 @@ True
False
# 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.set_publications([p1.id, p2.id])
True

View File

@ -24,12 +24,12 @@ class Article(meta.Model):
API_TESTS = """
# 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()
# Create an Article.
>>> 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.reporter_id
@ -64,26 +64,26 @@ This is a test
# Use double underscores to separate relationships.
# This works as many levels deep as you want. There's no limit.
# 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]
# 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]
# 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):
...
TypeError: got unexpected keyword argument 'reporter_id__exact'
# "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]
# You can also instantiate an Article by passing
# 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.reporter_id
1
@ -91,7 +91,7 @@ TypeError: got unexpected keyword argument 'reporter_id__exact'
John Smith
# 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.get_reporter()
John Smith

View File

@ -22,11 +22,11 @@ class Article(meta.Model):
API_TESTS = """
# Create a Reporter.
>>> r = reporters.Reporter(name='John Smith')
>>> r = Reporter(name='John Smith')
>>> r.save()
# Create an Article.
>>> a = articles.Article(headline="First", reporter=r)
>>> a = Article(headline="First", reporter=r)
>>> a.save()
>>> a.reporter_id
@ -54,14 +54,14 @@ First
2
# 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.id
3
>>> a3.reporter_id
>>> print a3.reporter_id
None
>>> a3 = articles.get_object(pk=3)
>>> a3 = Article.objects.get_object(pk=3)
>>> print a3.reporter_id
None
@ -70,9 +70,9 @@ None
>>> a3.get_reporter()
Traceback (most recent call last):
...
ReporterDoesNotExist
DoesNotExist
# 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]
"""

View File

@ -32,13 +32,13 @@ class Waiter(meta.Model):
API_TESTS = """
# 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()
>>> p2 = places.Place(name='Ace Hardware', address='1013 N. Ashland')
>>> p2 = Place(name='Ace Hardware', address='1013 N. Ashland')
>>> p2.save()
# 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()
# A Restaurant can access its place.
@ -53,20 +53,20 @@ Demon Dogs the restaurant
>>> p2.get_restaurant()
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.
>>> restaurants.get_list()
# Restaurant.objects.get_list() just returns the Restaurants, not the Places.
>>> Restaurant.objects.get_list()
[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.
>>> places.get_list(order_by=['name'])
>>> Place.objects.get_list(order_by=['name'])
[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
>>> restaurants.get_object(pk=1)
>>> Restaurant.objects.get_object(pk=1)
Demon Dogs the restaurant
# Add a Waiter to the Restaurant.
@ -75,6 +75,6 @@ Demon Dogs the restaurant
>>> w
Joe the waiter at Demon Dogs the restaurant
>>> r = restaurants.get_object(pk=1)
>>> r = Restaurant.objects.get_object(pk=1)
>>> r.delete()
"""

View File

@ -21,37 +21,37 @@ API_TESTS = """
>>> from datetime import datetime
>>> 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()
>>> a2 = articles.Article(headline='Goodbye', pub_date=datetime(2005, 11, 28))
>>> a2 = Article(headline='Goodbye', pub_date=datetime(2005, 11, 28))
>>> 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()
>>> 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]
>>> 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]
>>> 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]
>>> 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]
>>> 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]
>>> articles.get_list(complex=(Q(pk=1) | Q(pk=2)))
>>> Article.objects.get_list(complex=(Q(pk=1) | Q(pk=2)))
[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]
"""

View File

@ -27,37 +27,37 @@ class Article(meta.Model):
API_TESTS = """
# Create a couple of Articles.
>>> 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()
>>> 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()
>>> 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()
>>> 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()
# By default, articles.get_list() orders by pub_date descending, then
# headline ascending.
>>> articles.get_list()
>>> Article.objects.get_list()
[Article 4, Article 2, Article 3, Article 1]
# Override ordering with order_by, which is in the same format as the ordering
# attribute in models.
>>> articles.get_list(order_by=['headline'])
>>> Article.objects.get_list(order_by=['headline'])
[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]
# 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]
# 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]
# Use '?' to order randomly. (We're using [...] in the output to indicate we
# don't know what order the output will be in.
>>> articles.get_list(order_by=['?'])
>>> Article.objects.get_list(order_by=['?'])
[...]
"""

View File

@ -20,7 +20,7 @@ class Article(meta.Model):
API_TESTS = """
# Create an Article.
>>> 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()
>>> repr(a)

View File

@ -25,23 +25,23 @@ class Thing(meta.Model):
return self.when
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()
>>> print t.when
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()
>>> print u.when
h
>>> things.get_list(order_by=['when'])
>>> Thing.objects.get_list(order_by=['when'])
[a, h]
>>> v = things.get_object(pk='a')
>>> v = Thing.objects.get_object(pk='a')
>>> print v.join
b
>>> print v.where
g
>>> things.get_list(order_by=['select.when'])
>>> Thing.objects.get_list(order_by=['select.when'])
[a, h]
"""

View File

@ -32,18 +32,18 @@ class Person(meta.Model):
print "After deletion"
API_TESTS = """
>>> p1 = persons.Person(first_name='John', last_name='Smith')
>>> p1 = Person(first_name='John', last_name='Smith')
>>> p1.save()
Before save
After save
>>> persons.get_list()
>>> Person.objects.get_list()
[John Smith]
>>> p1.delete()
Before deletion
After deletion
>>> persons.get_list()
>>> Person.objects.get_list()
[]
"""

View File

@ -63,16 +63,16 @@ class NoModuleNameSecond(Article):
API_TESTS = """
# 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.
>>> 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
<ArticleWithSection object>
@ -84,7 +84,7 @@ API_TESTS = """
datetime.datetime(2005, 8, 22, 0, 0)
# 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
'First'
>>> a1.pub_date
@ -93,7 +93,7 @@ datetime.datetime(2005, 8, 22, 0, 0)
'News'
# Create an ArticleWithoutPubDate.
>>> a2 = subarticles2.ArticleWithoutPubDate(headline='Second')
>>> a2 = ArticleWithoutPubDate(headline='Second')
>>> a2.save()
>>> a2
<ArticleWithoutPubDate object>
@ -105,7 +105,7 @@ 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 = ArticleWithoutPubDate.objects.get_object(pk=1)
>>> a2.headline
'Second'
>>> a2.pub_date
@ -114,7 +114,7 @@ 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 = ArticleWithFieldOverride(headline='Third', pub_date=date(2005, 8, 22))
>>> a3.save()
>>> a3
<ArticleWithFieldOverride object>
@ -124,14 +124,14 @@ AttributeError: 'ArticleWithoutPubDate' object has no attribute 'pub_date'
datetime.date(2005, 8, 22)
# 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
'Third'
>>> a3.pub_date
datetime.date(2005, 8, 22)
# 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))
>>> a4.save()
@ -140,7 +140,7 @@ datetime.date(2005, 8, 22)
Fourth
# 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
'Fourth'
>>> a4.section
@ -151,26 +151,26 @@ True
datetime.date(2005, 8, 22)
# Test get_list().
>>> subarticles1.get_list()
>>> ArticleWithSection.objects.get_list()
[<ArticleWithSection object>]
>>> subarticles2.get_list()
>>> ArticleWithoutPubDate.objects.get_list()
[<ArticleWithoutPubDate object>]
>>> subarticles3.get_list()
>>> ArticleWithFieldOverride.objects.get_list()
[<ArticleWithFieldOverride object>]
>>> subarticles4.get_list()
>>> ArticleWithManyChanges.objects.get_list()
[Fourth]
# 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()
>>> a6 = subarticles5.ArticleWithChangedMeta(headline='B', pub_date=datetime(2005, 4, 1))
>>> a6 = ArticleWithChangedMeta(headline='B', pub_date=datetime(2005, 4, 1))
>>> 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()
# Ordering has been overridden, so objects are ordered
# by headline ASC instead of pub_date DESC.
>>> subarticles5.get_list()
>>> ArticleWithChangedMeta.objects.get_list()
[A, B, C]
>>> nomodulenamefirsts.get_list()