magic-removal: Updated some of the unit tests to use new syntax. 175 unit-test failures at this point.
git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2157 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
83f8870489
commit
2be3eefdff
|
@ -18,8 +18,7 @@ API_TESTS = """
|
|||
|
||||
# Create an Article.
|
||||
>>> from datetime import datetime
|
||||
>>> a = Article(id=None, headline='Area man programs in Python',
|
||||
... pub_date=datetime(2005, 7, 28))
|
||||
>>> 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.
|
||||
>>> a.save()
|
||||
|
@ -105,8 +104,7 @@ True
|
|||
datetime.datetime(2005, 7, 29, 0, 0)
|
||||
|
||||
# ...or, you can use keyword arguments.
|
||||
>>> a3 = Article(id=None, headline='Third article',
|
||||
... pub_date=datetime(2005, 7, 30))
|
||||
>>> a3 = Article(id=None, headline='Third article', pub_date=datetime(2005, 7, 30))
|
||||
>>> a3.save()
|
||||
>>> a3.id
|
||||
3L
|
||||
|
@ -176,28 +174,26 @@ True
|
|||
>>> Article.objects.get(id__exact=8) == Article.objects.get(id__exact=7)
|
||||
False
|
||||
|
||||
## TODO - what should these be converted to?
|
||||
>>> Article.objects.get_pub_date_list('year')
|
||||
[datetime.datetime(2005, 1, 1, 0, 0)]
|
||||
>>> Article.objects.get_pub_date_list('month')
|
||||
[datetime.datetime(2005, 7, 1, 0, 0)]
|
||||
>>> Article.objects.get_pub_date_list('day')
|
||||
[datetime.datetime(2005, 7, 28, 0, 0), datetime.datetime(2005, 7, 29, 0, 0), datetime.datetime(2005, 7, 30, 0, 0), datetime.datetime(2005, 7, 31, 0, 0)]
|
||||
>>> Article.objects.get_pub_date_list('day', order='ASC')
|
||||
[datetime.datetime(2005, 7, 28, 0, 0), datetime.datetime(2005, 7, 29, 0, 0), datetime.datetime(2005, 7, 30, 0, 0), datetime.datetime(2005, 7, 31, 0, 0)]
|
||||
>>> Article.objects.get_pub_date_list('day', order='DESC')
|
||||
[datetime.datetime(2005, 7, 31, 0, 0), datetime.datetime(2005, 7, 30, 0, 0), datetime.datetime(2005, 7, 29, 0, 0), datetime.datetime(2005, 7, 28, 0, 0)]
|
||||
|
||||
##>>> Article.objects.get_pub_date_list('year')
|
||||
##[datetime.datetime(2005, 1, 1, 0, 0)]
|
||||
##>>> Article.objects.get_pub_date_list('month')
|
||||
##[datetime.datetime(2005, 7, 1, 0, 0)]
|
||||
##>>> Article.objects.get_pub_date_list('day')
|
||||
##[datetime.datetime(2005, 7, 28, 0, 0), datetime.datetime(2005, 7, 29, 0, 0), datetime.datetime(2005, 7, 30, 0, 0), datetime.datetime(2005, 7, 31, 0, 0)]
|
||||
##>>> Article.objects.get_pub_date_list('day', order='ASC')
|
||||
##[datetime.datetime(2005, 7, 28, 0, 0), datetime.datetime(2005, 7, 29, 0, 0), datetime.datetime(2005, 7, 30, 0, 0), datetime.datetime(2005, 7, 31, 0, 0)]
|
||||
##>>> Article.objects.get_pub_date_list('day', order='DESC')
|
||||
##[datetime.datetime(2005, 7, 31, 0, 0), datetime.datetime(2005, 7, 30, 0, 0), datetime.datetime(2005, 7, 29, 0, 0), datetime.datetime(2005, 7, 28, 0, 0)]
|
||||
##
|
||||
### Try some bad arguments to __get_date_list
|
||||
##>>> Article.objects.get_pub_date_list('badarg')
|
||||
##Traceback (most recent call last):
|
||||
## ...
|
||||
##AssertionError: 'kind' must be one of 'year', 'month' or 'day'.
|
||||
##>>> Article.objects.get_pub_date_list(order='ASC')
|
||||
##Traceback (most recent call last):
|
||||
## ...
|
||||
##TypeError: __get_date_list() takes at least 3 non-keyword arguments (2 given)
|
||||
# Try some bad arguments to __get_date_list
|
||||
>>> Article.objects.get_pub_date_list('badarg')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AssertionError: 'kind' must be one of 'year', 'month' or 'day'.
|
||||
>>> Article.objects.get_pub_date_list(order='ASC')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: __get_date_list() takes at least 3 non-keyword arguments (2 given)
|
||||
|
||||
# You can combine queries with & and |
|
||||
>>> s1 = Article.objects.filter(id__exact=1)
|
||||
|
@ -213,7 +209,7 @@ False
|
|||
>>> len(Article.objects.filter(id__exact=1))
|
||||
1
|
||||
|
||||
# You can get items using index and slice notation:
|
||||
# You can get items using index and slice notation.
|
||||
>>> Article.objects[0]
|
||||
<Article object>
|
||||
>>> Article.objects[1:2]
|
||||
|
@ -222,7 +218,6 @@ False
|
|||
>>> (s1 | s2 | s3)[::2]
|
||||
[<Article object>, <Article object>]
|
||||
|
||||
|
||||
# An Article instance doesn't have access to the "objects" attribute.
|
||||
# That is only available as a class method.
|
||||
>>> list(a7.objects)
|
||||
|
|
|
@ -29,63 +29,63 @@ class Business(models.Model):
|
|||
API_TESTS = """
|
||||
>>> dan = Employee(employee_code='ABC123', first_name='Dan', last_name='Jones')
|
||||
>>> dan.save()
|
||||
>>> Employee.objects.get_list()
|
||||
>>> list(Employee.objects)
|
||||
[Dan Jones]
|
||||
|
||||
>>> fran = Employee(employee_code='XYZ456', first_name='Fran', last_name='Bones')
|
||||
>>> fran.save()
|
||||
>>> Employee.objects.get_list()
|
||||
>>> list(Employee.objects)
|
||||
[Fran Bones, Dan Jones]
|
||||
|
||||
>>> Employee.objects.get_object(pk='ABC123')
|
||||
>>> Employee.objects.get(pk='ABC123')
|
||||
Dan Jones
|
||||
>>> Employee.objects.get_object(pk='XYZ456')
|
||||
>>> Employee.objects.get(pk='XYZ456')
|
||||
Fran Bones
|
||||
>>> Employee.objects.get_object(pk='foo')
|
||||
>>> Employee.objects.get(pk='foo')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
DoesNotExist: Employee does not exist for {'pk': 'foo'}
|
||||
|
||||
# Use the name of the primary key, rather than pk.
|
||||
>>> Employee.objects.get_object(employee_code__exact='ABC123')
|
||||
>>> Employee.objects.get(employee_code__exact='ABC123')
|
||||
Dan Jones
|
||||
|
||||
# Fran got married and changed her last name.
|
||||
>>> fran = Employee.objects.get_object(pk='XYZ456')
|
||||
>>> fran = Employee.objects.get(pk='XYZ456')
|
||||
>>> fran.last_name = 'Jones'
|
||||
>>> fran.save()
|
||||
>>> Employee.objects.get_list(last_name__exact='Jones')
|
||||
>>> list(Employee.objects.filter(last_name__exact='Jones'))
|
||||
[Dan Jones, Fran Jones]
|
||||
>>> Employee.objects.get_in_bulk(['ABC123', 'XYZ456'])
|
||||
>>> Employee.objects.in_bulk(['ABC123', 'XYZ456'])
|
||||
{'XYZ456': Fran Jones, 'ABC123': Dan Jones}
|
||||
|
||||
>>> b = Business(name='Sears')
|
||||
>>> b.save()
|
||||
>>> b.set_employees([dan.employee_code, fran.employee_code])
|
||||
True
|
||||
>>> b.get_employee_list()
|
||||
>>> list(b.employee_set)
|
||||
[Dan Jones, Fran Jones]
|
||||
>>> fran.get_business_list()
|
||||
>>> list(fran.business_set)
|
||||
[Sears]
|
||||
>>> Business.objects.get_in_bulk(['Sears'])
|
||||
>>> Business.objects.in_bulk(['Sears'])
|
||||
{'Sears': Sears}
|
||||
|
||||
>>> Business.objects.get_list(name__exact='Sears')
|
||||
>>> list(Business.objects.filter(name__exact='Sears'))
|
||||
[Sears]
|
||||
>>> Business.objects.get_list(pk='Sears')
|
||||
>>> list(Business.objects.filter(pk='Sears'))
|
||||
[Sears]
|
||||
|
||||
# Queries across tables, involving primary key
|
||||
>>> Employee.objects.get_list(business__name__exact='Sears')
|
||||
>>> list(Employee.objects.filter(business__name__exact='Sears'))
|
||||
[Dan Jones, Fran Jones]
|
||||
>>> Employee.objects.get_list(business__pk='Sears')
|
||||
>>> list(Employee.objects.filter(business__pk='Sears'))
|
||||
[Dan Jones, Fran Jones]
|
||||
|
||||
>>> Business.objects.get_list(employees__employee_code__exact='ABC123')
|
||||
>>> list(Business.objects.filter(employees__employee_code__exact='ABC123'))
|
||||
[Sears]
|
||||
>>> Business.objects.get_list(employees__pk='ABC123')
|
||||
>>> list(Business.objects.filter(employees__pk='ABC123'))
|
||||
[Sears]
|
||||
>>> Business.objects.get_list(employees__first_name__startswith='Fran')
|
||||
>>> list(Business.objects.filter(employees__first_name__startswith='Fran'))
|
||||
[Sears]
|
||||
|
||||
"""
|
||||
|
|
|
@ -49,38 +49,38 @@ Article 1
|
|||
... print a.headline
|
||||
Article 4
|
||||
|
||||
# get_count() returns the number of objects matching search criteria.
|
||||
>>> Article.objects.get_count()
|
||||
# count() returns the number of objects matching search criteria.
|
||||
>>> Article.objects.count()
|
||||
7L
|
||||
>>> Article.objects.get_count(pub_date__exact=datetime(2005, 7, 27))
|
||||
>>> Article.objects.filter(pub_date__exact=datetime(2005, 7, 27)).count()
|
||||
3L
|
||||
>>> Article.objects.get_count(headline__startswith='Blah blah')
|
||||
>>> Article.objects.filter(headline__startswith='Blah blah').count()
|
||||
0L
|
||||
|
||||
# get_in_bulk() takes a list of IDs and returns a dictionary mapping IDs
|
||||
# in_bulk() takes a list of IDs and returns a dictionary mapping IDs
|
||||
# to objects.
|
||||
>>> Article.objects.get_in_bulk([1, 2])
|
||||
>>> Article.objects.in_bulk([1, 2])
|
||||
{1: Article 1, 2: Article 2}
|
||||
>>> Article.objects.get_in_bulk([3])
|
||||
>>> Article.objects.in_bulk([3])
|
||||
{3: Article 3}
|
||||
>>> Article.objects.get_in_bulk([1000])
|
||||
>>> Article.objects.in_bulk([1000])
|
||||
{}
|
||||
>>> Article.objects.get_in_bulk([])
|
||||
>>> Article.objects.in_bulk([])
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AssertionError: get_in_bulk() cannot be passed an empty ID list.
|
||||
>>> Article.objects.get_in_bulk('foo')
|
||||
AssertionError: in_bulk() cannot be passed an empty ID list.
|
||||
>>> Article.objects.in_bulk('foo')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AssertionError: get_in_bulk() must be provided with a list of IDs.
|
||||
>>> Article.objects.get_in_bulk()
|
||||
AssertionError: in_bulk() must be provided with a list of IDs.
|
||||
>>> Article.objects.in_bulk()
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: get_in_bulk() takes at least 2 arguments (1 given)
|
||||
>>> Article.objects.get_in_bulk(headline__startswith='Blah')
|
||||
TypeError: in_bulk() takes at least 2 arguments (1 given)
|
||||
>>> Article.objects.in_bulk(headline__startswith='Blah')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: get_in_bulk() takes at least 2 non-keyword arguments (1 given)
|
||||
TypeError: in_bulk() takes at least 2 non-keyword arguments (1 given)
|
||||
|
||||
# get_values() is just like get_list(), except it returns a list of
|
||||
# dictionaries instead of object instances -- and you can specify which fields
|
||||
|
@ -143,14 +143,14 @@ Article 1
|
|||
# database library, but Django handles the quoting of them automatically.
|
||||
>>> a8 = Article(headline='Article_ with underscore', pub_date=datetime(2005, 11, 20))
|
||||
>>> a8.save()
|
||||
>>> Article.objects.get_list(headline__startswith='Article')
|
||||
>>> list(Article.objects.filter(headline__startswith='Article'))
|
||||
[Article_ with underscore, Article 5, Article 6, Article 4, Article 2, Article 3, Article 7, Article 1]
|
||||
>>> Article.objects.get_list(headline__startswith='Article_')
|
||||
>>> list(Article.objects.filter(headline__startswith='Article_'))
|
||||
[Article_ with underscore]
|
||||
>>> a9 = Article(headline='Article% with percent sign', pub_date=datetime(2005, 11, 21))
|
||||
>>> a9.save()
|
||||
>>> Article.objects.get_list(headline__startswith='Article')
|
||||
>>> list(Article.objects.filter(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.objects.get_list(headline__startswith='Article%')
|
||||
>>> list(Article.objects.filter(headline__startswith='Article%'))
|
||||
[Article% with percent sign]
|
||||
"""
|
||||
|
|
|
@ -53,16 +53,16 @@ API_TESTS = """
|
|||
>>> w2.save()
|
||||
|
||||
# Play around with the API.
|
||||
>>> a.get_writer_list(order_by=['-position'], select_related=True)
|
||||
>>> list(a.writer_set.order_by('-position').extra(select_related=True))
|
||||
[John Smith (Main writer), Jane Doe (Contributor)]
|
||||
>>> w1.get_reporter()
|
||||
>>> w1.reporter
|
||||
John Smith
|
||||
>>> w2.get_reporter()
|
||||
>>> w2.reporter
|
||||
Jane Doe
|
||||
>>> w1.get_article()
|
||||
>>> w1.article
|
||||
This is a test
|
||||
>>> w2.get_article()
|
||||
>>> w2.article
|
||||
This is a test
|
||||
>>> r1.get_writer_list()
|
||||
>>> list(r1.writer_set)
|
||||
[John Smith (Main writer)]
|
||||
"""
|
||||
|
|
|
@ -63,36 +63,36 @@ True
|
|||
# specified the "singular" parameter, Django would just use "category", which
|
||||
# would cause a conflict because the "primary_categories" and
|
||||
# "secondary_categories" fields both relate to Category.
|
||||
>>> a1.get_primary_category_list()
|
||||
>>> list(a1.primary_category_set)
|
||||
[Crime, News]
|
||||
|
||||
# Ditto for the "primary_category" here.
|
||||
>>> a2.get_primary_category_list()
|
||||
>>> list(a2.primary_category_set)
|
||||
[News, Sports]
|
||||
|
||||
# Ditto for the "secondary_category" here.
|
||||
>>> a1.get_secondary_category_list()
|
||||
>>> list(a1.secondary_category_set)
|
||||
[Life]
|
||||
|
||||
# Ditto for the "secondary_category" here.
|
||||
>>> a2.get_secondary_category_list()
|
||||
>>> list(a2.secondary_category)
|
||||
[Life]
|
||||
|
||||
|
||||
>>> c1.get_primary_article_list()
|
||||
>>> list(c1.primary_article_set)
|
||||
[Area man runs]
|
||||
>>> c1.get_secondary_article_list()
|
||||
>>> list(c1.secondary_article_set)
|
||||
[]
|
||||
>>> c2.get_primary_article_list()
|
||||
>>> list(c2.primary_article_set)
|
||||
[Area man steals, Area man runs]
|
||||
>>> c2.get_secondary_article_list()
|
||||
>>> list(c2.secondary_article_set)
|
||||
[]
|
||||
>>> c3.get_primary_article_list()
|
||||
>>> list(c3.primary_article_set)
|
||||
[Area man steals]
|
||||
>>> c3.get_secondary_article_list()
|
||||
>>> list(c3.secondary_article_set)
|
||||
[]
|
||||
>>> c4.get_primary_article_list()
|
||||
>>> list(c4.primary_article_set)
|
||||
[]
|
||||
>>> c4.get_secondary_article_list()
|
||||
>>> list(c4.secondary_article_set)
|
||||
[Area man steals, Area man runs]
|
||||
"""
|
||||
|
|
|
@ -26,17 +26,17 @@ API_TESTS = """
|
|||
>>> c = Category(id=None, name='Child category', parent=r)
|
||||
>>> c.save()
|
||||
|
||||
>>> r.get_child_list()
|
||||
>>> list(r.child_set)
|
||||
[Child category]
|
||||
>>> r.get_child(name__startswith='Child')
|
||||
>>> r.child_set.get(name__startswith='Child')
|
||||
Child category
|
||||
>>> r.get_parent()
|
||||
>>> r.parent
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
DoesNotExist
|
||||
|
||||
>>> c.get_child_list()
|
||||
>>> list(c.child_set)
|
||||
[]
|
||||
>>> c.get_parent()
|
||||
>>> c.parent
|
||||
Root category
|
||||
"""
|
||||
|
|
|
@ -28,16 +28,16 @@ API_TESTS = """
|
|||
>>> kid = Person(full_name='John Smith Junior', mother=mom, father=dad)
|
||||
>>> kid.save()
|
||||
|
||||
>>> kid.get_mother()
|
||||
>>> kid.mother
|
||||
Jane Smith
|
||||
>>> kid.get_father()
|
||||
>>> kid.father
|
||||
John Smith Senior
|
||||
>>> dad.get_fathers_child_list()
|
||||
>>> list(dad.fathers_child_set)
|
||||
[John Smith Junior]
|
||||
>>> mom.get_mothers_child_list()
|
||||
>>> list(mom.mothers_child_set)
|
||||
[John Smith Junior]
|
||||
>>> kid.get_mothers_child_list()
|
||||
>>> list(kid.mothers_child_set)
|
||||
[]
|
||||
>>> kid.get_fathers_child_list()
|
||||
>>> list(kid.fathers_child_set)
|
||||
[]
|
||||
"""
|
||||
|
|
|
@ -32,7 +32,7 @@ API_TESTS = """
|
|||
>>> m1 = man.save(data)
|
||||
|
||||
# Verify it worked.
|
||||
>>> Musician.objects.get_list()
|
||||
>>> list(Musician.objects)
|
||||
[Ella Fitzgerald]
|
||||
>>> [m1] == Musician.objects.get_list()
|
||||
True
|
||||
|
@ -66,9 +66,9 @@ True
|
|||
>>> a1 = man.save(data)
|
||||
|
||||
# Verify it worked.
|
||||
>>> Album.objects.get_list()
|
||||
>>> list(Album.objects)
|
||||
[Ella and Basie]
|
||||
>>> Album.objects.get_object().get_musician()
|
||||
>>> Album.objects.get().musician
|
||||
Ella Fitzgerald
|
||||
|
||||
# Create an Album with a release_date.
|
||||
|
@ -79,9 +79,9 @@ Ella Fitzgerald
|
|||
>>> a2 = man.save(data)
|
||||
|
||||
# Verify it worked.
|
||||
>>> Album.objects.get_list(order_by=['name'])
|
||||
>>> list(Album.objects.filter(order_by=['name']))
|
||||
[Ella and Basie, Ultimate Ella]
|
||||
>>> a2 = Album.objects.get_object(pk=2)
|
||||
>>> a2 = Album.objects.get(pk=2)
|
||||
>>> a2
|
||||
Ultimate Ella
|
||||
>>> a2.release_date
|
||||
|
|
|
@ -56,56 +56,56 @@ True
|
|||
True
|
||||
|
||||
# Article objects have access to their related Publication objects.
|
||||
>>> a1.get_publication_list()
|
||||
>>> list(a1.publication_set)
|
||||
[The Python Journal]
|
||||
>>> a2.get_publication_list()
|
||||
>>> list(a2.publication_set)
|
||||
[The Python Journal, Science News, Science Weekly]
|
||||
|
||||
# Publication objects have access to their related Article objects.
|
||||
>>> p2.get_article_list()
|
||||
>>> list(p2.article_set)
|
||||
[NASA uses Python]
|
||||
>>> p1.get_article_list(order_by=['headline'])
|
||||
>>> list(p1.article_set.order_by('headline'))
|
||||
[Django lets you build Web apps easily, NASA uses Python]
|
||||
|
||||
# We can perform kwarg queries across m2m relationships
|
||||
>>> Article.objects.get_list(publications__id__exact=1)
|
||||
>>> list(Article.objects.filter(publications__id__exact=1))
|
||||
[Django lets you build Web apps easily, NASA uses Python]
|
||||
>>> Article.objects.get_list(publications__pk=1)
|
||||
>>> list(Article.objects.filter(publications__pk=1))
|
||||
[Django lets you build Web apps easily, NASA uses Python]
|
||||
|
||||
>>> Article.objects.get_list(publications__title__startswith="Science")
|
||||
>>> list(Article.objects.filter(publications__title__startswith="Science"))
|
||||
[NASA uses Python, NASA uses Python]
|
||||
|
||||
>>> Article.objects.get_list(publications__title__startswith="Science", distinct=True)
|
||||
>>> list(Article.objects.filter(publications__title__startswith="Science", distinct=True))
|
||||
[NASA uses Python]
|
||||
|
||||
# Reverse m2m queries (i.e., start at the table that doesn't have a ManyToManyField)
|
||||
>>> Publication.objects.get_list(id__exact=1)
|
||||
>>> list(Publication.objects.filter(id__exact=1))
|
||||
[The Python Journal]
|
||||
>>> Publication.objects.get_list(pk=1)
|
||||
>>> list(Publication.objects.filter(pk=1))
|
||||
[The Python Journal]
|
||||
|
||||
>>> Publication.objects.get_list(article__headline__startswith="NASA")
|
||||
>>> list(Publication.objects.filter(article__headline__startswith="NASA"))
|
||||
[The Python Journal, Science News, Science Weekly]
|
||||
|
||||
>>> Publication.objects.get_list(article__id__exact=1)
|
||||
>>> list(Publication.objects.filter(article__id__exact=1))
|
||||
[The Python Journal]
|
||||
|
||||
>>> Publication.objects.get_list(article__pk=1)
|
||||
>>> list(Publication.objects.filter(article__pk=1))
|
||||
[The Python Journal]
|
||||
|
||||
# If we delete a Publication, its Articles won't be able to access it.
|
||||
>>> p1.delete()
|
||||
>>> Publication.objects.get_list()
|
||||
>>> list(Publication.objects)
|
||||
[Science News, Science Weekly]
|
||||
>>> a1 = Article.objects.get_object(pk=1)
|
||||
>>> a1.get_publication_list()
|
||||
>>> a1 = Article.objects.get(pk=1)
|
||||
>>> list(a1.publication_set)
|
||||
[]
|
||||
|
||||
# If we delete an Article, its Publications won't be able to access it.
|
||||
>>> a2.delete()
|
||||
>>> Article.objects.get_list()
|
||||
>>> list(Article.objects)
|
||||
[Django lets you build Web apps easily]
|
||||
>>> p1.get_article_list(order_by=['headline'])
|
||||
>>> list(p1.article_set.order_by=('headline'))
|
||||
[Django lets you build Web apps easily]
|
||||
"""
|
||||
|
|
|
@ -36,60 +36,60 @@ API_TESTS = """
|
|||
>>> a = Article(id=None, headline="This is a test", pub_date=datetime(2005, 7, 27), reporter=r)
|
||||
>>> a.save()
|
||||
|
||||
>>> a.reporter_id
|
||||
>>> a.reporter.id
|
||||
1
|
||||
|
||||
>>> a.get_reporter()
|
||||
>>> a.reporter
|
||||
John Smith
|
||||
|
||||
# Article objects have access to their related Reporter objects.
|
||||
>>> r = a.get_reporter()
|
||||
>>> r = a.reporter
|
||||
>>> r.first_name, r.last_name
|
||||
('John', 'Smith')
|
||||
|
||||
# Create an Article via the Reporter object.
|
||||
>>> new_article = r.add_article(headline="John's second story", pub_date=datetime(2005, 7, 29))
|
||||
>>> new_article = r.article_set.add(headline="John's second story", pub_date=datetime(2005, 7, 29))
|
||||
>>> new_article
|
||||
John's second story
|
||||
>>> new_article.reporter_id
|
||||
>>> new_article.reporter.id
|
||||
1
|
||||
|
||||
>>> new_article2 = r2.add_article(headline="Paul's story", pub_date=datetime(2006, 1, 17))
|
||||
>>> new_article2.reporter_id
|
||||
>>> new_article2 = r2.article_set.add(headline="Paul's story", pub_date=datetime(2006, 1, 17))
|
||||
>>> new_article2.reporter.id
|
||||
2
|
||||
|
||||
# Reporter objects have access to their related Article objects.
|
||||
>>> r.get_article_list(order_by=['pub_date'])
|
||||
>>> list(r.article_set.order_by('pub_date'))
|
||||
[This is a test, John's second story]
|
||||
|
||||
>>> r.get_article(headline__startswith='This')
|
||||
>>> list(r.article_set.filter(headline__startswith='This'))
|
||||
This is a test
|
||||
|
||||
>>> r.get_article_count()
|
||||
>>> r.article_set.count()
|
||||
2
|
||||
|
||||
>>> r2.get_article_count()
|
||||
>>> r2.article_set.count()
|
||||
1
|
||||
|
||||
# Get articles by id
|
||||
>>> Article.objects.get_list(id__exact=1)
|
||||
>>> list(Article.objects.filter(id__exact=1))
|
||||
[This is a test]
|
||||
>>> Article.objects.get_list(pk=1)
|
||||
>>> list(Article.objects.filter(pk=1))
|
||||
[This is a test]
|
||||
|
||||
# Query on an article property
|
||||
>>> Article.objects.get_list(headline__startswith='This')
|
||||
>>> list(Article.objects.filter(headline__startswith='This'))
|
||||
[This is a test]
|
||||
|
||||
# The API automatically follows relationships as far as you need.
|
||||
# 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".
|
||||
>>> Article.objects.get_list(reporter__first_name__exact='John', order_by=['pub_date'])
|
||||
>>> list(Article.objects.filter(reporter__first_name__exact='John').order_by('pub_date'))
|
||||
[This is a test, John's second story]
|
||||
|
||||
# Query twice over the related field.
|
||||
>>> Article.objects.get_list(reporter__first_name__exact='John', reporter__last_name__exact='Smith')
|
||||
>>> list(Article.objects.filter(reporter__first_name__exact='John', reporter__last_name__exact='Smith'))
|
||||
[This is a test, John's second story]
|
||||
|
||||
# The underlying query only makes one join when a related table is referenced twice.
|
||||
|
@ -98,68 +98,68 @@ This is a test
|
|||
1
|
||||
|
||||
# The automatically joined table has a predictable name.
|
||||
>>> Article.objects.get_list(reporter__first_name__exact='John', where=["many_to_one_article__reporter.last_name='Smith'"])
|
||||
>>> list(Article.objects.filter(reporter__first_name__exact='John').extra(where=["many_to_one_article__reporter.last_name='Smith'"]))
|
||||
[This is a test, John's second story]
|
||||
|
||||
# Find all Articles for the Reporter whose ID is 1.
|
||||
>>> Article.objects.get_list(reporter__id__exact=1, order_by=['pub_date'])
|
||||
>>> list(Article.objects.filter(reporter__id__exact=1).order_by('pub_date'))
|
||||
[This is a test, John's second story]
|
||||
>>> Article.objects.get_list(reporter__pk=1, order_by=['pub_date'])
|
||||
>>> list(Article.objects.filter(reporter__pk=1).order_by('pub_date'))
|
||||
[This is a test, John's second story]
|
||||
|
||||
# You need two underscores between "reporter" and "id" -- not one.
|
||||
>>> Article.objects.get_list(reporter_id__exact=1)
|
||||
>>> list(Article.objects.filter(reporter_id__exact=1))
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: Cannot resolve keyword 'reporter_id' into field
|
||||
|
||||
# You need to specify a comparison clause
|
||||
>>> Article.objects.get_list(reporter_id=1)
|
||||
>>> list(Article.objects.filter(reporter_id=1))
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: Cannot parse keyword query 'reporter_id'
|
||||
|
||||
# "pk" shortcut syntax works in a related context, too.
|
||||
>>> Article.objects.get_list(reporter__pk=1, order_by=['pub_date'])
|
||||
>>> list(Article.objects.filter(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 = Article(id=None, headline="This is a test", pub_date=datetime(2005, 7, 27), reporter_id=r.id)
|
||||
>>> a3.save()
|
||||
>>> a3.reporter_id
|
||||
>>> a3.reporter.id
|
||||
1
|
||||
>>> a3.get_reporter()
|
||||
>>> a3.reporter
|
||||
John Smith
|
||||
|
||||
# Similarly, the reporter ID can be a string.
|
||||
>>> a4 = Article(id=None, headline="This is a test", pub_date=datetime(2005, 7, 27), reporter_id="1")
|
||||
>>> a4.save()
|
||||
>>> a4.get_reporter()
|
||||
>>> a4.reporter
|
||||
John Smith
|
||||
|
||||
# Reporters can be queried
|
||||
>>> Reporter.objects.get_list(id__exact=1)
|
||||
>>> list(Reporter.objects.filter(id__exact=1))
|
||||
[John Smith]
|
||||
>>> Reporter.objects.get_list(pk=1)
|
||||
>>> list(Reporter.objects.filter(pk=1))
|
||||
[John Smith]
|
||||
>>> Reporter.objects.get_list(first_name__startswith='John')
|
||||
>>> list(Reporter.objects.filter(first_name__startswith='John'))
|
||||
[John Smith]
|
||||
|
||||
# Reporters can query in opposite direction of ForeignKey definition
|
||||
>>> Reporter.objects.get_list(article__id__exact=1)
|
||||
>>> list(Reporter.objects.filter(article__id__exact=1))
|
||||
[John Smith]
|
||||
>>> Reporter.objects.get_list(article__pk=1)
|
||||
>>> list(Reporter.objects.filter(article__pk=1))
|
||||
[John Smith]
|
||||
>>> Reporter.objects.get_list(article__headline__startswith='This')
|
||||
>>> list(Reporter.objects.filter(article__headline__startswith='This'))
|
||||
[John Smith, John Smith, John Smith]
|
||||
>>> Reporter.objects.get_list(article__headline__startswith='This', distinct=True)
|
||||
>>> list(Reporter.objects.filter(article__headline__startswith='This', distinct=True))
|
||||
[John Smith]
|
||||
|
||||
# Queries can go round in circles.
|
||||
>>> Reporter.objects.get_list(article__reporter__first_name__startswith='John')
|
||||
>>> list(Reporter.objects.filter(article__reporter__first_name__startswith='John'))
|
||||
[John Smith, John Smith, John Smith, John Smith]
|
||||
>>> Reporter.objects.get_list(article__reporter__first_name__startswith='John', distinct=True)
|
||||
>>> list(Reporter.objects.filter(article__reporter__first_name__startswith='John', distinct=True))
|
||||
[John Smith]
|
||||
|
||||
# Deletes that require joins are prohibited.
|
||||
|
@ -169,14 +169,14 @@ Traceback (most recent call last):
|
|||
TypeError: Joins are not allowed in this type of query
|
||||
|
||||
# If you delete a reporter, his articles will be deleted.
|
||||
>>> Article.objects.get_list(order_by=['headline'])
|
||||
>>> list(Article.objects.order_by('headline'))
|
||||
[John's second story, Paul's story, This is a test, This is a test, This is a test]
|
||||
>>> Reporter.objects.get_list(order_by=['first_name'])
|
||||
>>> list(Reporter.objects.order_by('first_name'))
|
||||
[John Smith, Paul Jones]
|
||||
>>> r.delete()
|
||||
>>> Article.objects.get_list(order_by=['headline'])
|
||||
>>> list(Article.objects.order_by('headline'))
|
||||
[Paul's story]
|
||||
>>> Reporter.objects.get_list(order_by=['first_name'])
|
||||
>>> list(Reporter.objects.order_by('first_name'))
|
||||
[Paul Jones]
|
||||
|
||||
"""
|
||||
|
|
|
@ -29,28 +29,28 @@ API_TESTS = """
|
|||
>>> a = Article(headline="First", reporter=r)
|
||||
>>> a.save()
|
||||
|
||||
>>> a.reporter_id
|
||||
>>> a.reporter.id
|
||||
1
|
||||
|
||||
>>> a.get_reporter()
|
||||
>>> a.reporter
|
||||
John Smith
|
||||
|
||||
# Article objects have access to their related Reporter objects.
|
||||
>>> r = a.get_reporter()
|
||||
>>> r = a.reporter
|
||||
|
||||
# Create an Article via the Reporter object.
|
||||
>>> a2 = r.add_article(headline="Second")
|
||||
>>> a2 = r.article_set.add(headline="Second")
|
||||
>>> a2
|
||||
Second
|
||||
>>> a2.reporter_id
|
||||
>>> a2.reporter.id
|
||||
1
|
||||
|
||||
# Reporter objects have access to their related Article objects.
|
||||
>>> r.get_article_list(order_by=['headline'])
|
||||
>>> list(r.article_set.order_by('headline'))
|
||||
[First, Second]
|
||||
>>> r.get_article(headline__startswith='Fir')
|
||||
>>> list(r.article_set.filter(headline__startswith='Fir'))
|
||||
First
|
||||
>>> r.get_article_count()
|
||||
>>> r.article_set.count()
|
||||
2
|
||||
|
||||
# Create an Article with no Reporter by passing "reporter=None".
|
||||
|
@ -58,21 +58,21 @@ First
|
|||
>>> a3.save()
|
||||
>>> a3.id
|
||||
3
|
||||
>>> a3.reporter_id
|
||||
>>> print a3.reporter_id
|
||||
>>> a3.reporter.id
|
||||
>>> print a3.reporter.id
|
||||
None
|
||||
>>> a3 = Article.objects.get_object(pk=3)
|
||||
>>> print a3.reporter_id
|
||||
>>> a3 = Article.objects.get(pk=3)
|
||||
>>> print a3.reporter.id
|
||||
None
|
||||
|
||||
# An article's get_reporter() method throws ReporterDoesNotExist
|
||||
# Accessing an article's 'reporter' attribute throws ReporterDoesNotExist
|
||||
# if the reporter is set to None.
|
||||
>>> a3.get_reporter()
|
||||
>>> a3.reporter
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
DoesNotExist
|
||||
|
||||
# To retrieve the articles with no reporters set, use "reporter__isnull=True".
|
||||
>>> Article.objects.get_list(reporter__isnull=True)
|
||||
>>> list(Article.objects.filter(reporter__isnull=True))
|
||||
[Third]
|
||||
"""
|
||||
|
|
|
@ -9,22 +9,22 @@ from django.db.models import *
|
|||
class Parent(Model):
|
||||
name = CharField(maxlength=100)
|
||||
bestchild = ForeignKey("Child", null=True, related_name="favoured_by")
|
||||
|
||||
|
||||
class Child(Model):
|
||||
name = CharField(maxlength=100)
|
||||
parent = ForeignKey(Parent)
|
||||
|
||||
|
||||
API_TESTS = """
|
||||
# Create a Parent
|
||||
>>> q = Parent(name='Elizabeth')
|
||||
>>> q.save()
|
||||
|
||||
# Create some children
|
||||
>>> c = q.add_child(name='Charles')
|
||||
>>> e = q.add_child(name='Edward')
|
||||
>>> c = q.child_set.add(name='Charles')
|
||||
>>> e = q.child_set.add(name='Edward')
|
||||
|
||||
# Set the best child
|
||||
>>> q.bestchild_id = c.id
|
||||
>>> q.bestchild = c
|
||||
>>> q.save()
|
||||
|
||||
>>> q.delete()
|
||||
|
|
|
@ -42,65 +42,65 @@ API_TESTS = """
|
|||
>>> r.save()
|
||||
|
||||
# A Restaurant can access its place.
|
||||
>>> r.get_place()
|
||||
>>> r.place
|
||||
Demon Dogs the place
|
||||
|
||||
# A Place can access its restaurant, if available.
|
||||
>>> p1.get_restaurant()
|
||||
>>> p1.restaurant
|
||||
Demon Dogs the restaurant
|
||||
|
||||
# p2 doesn't have an associated restaurant.
|
||||
>>> p2.get_restaurant()
|
||||
>>> p2.restaurant
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
DoesNotExist: Restaurant does not exist for {'place__id__exact': ...}
|
||||
|
||||
# Restaurant.objects.get_list() just returns the Restaurants, not the Places.
|
||||
>>> Restaurant.objects.get_list()
|
||||
>>> list(Restaurant.objects)
|
||||
[Demon Dogs the restaurant]
|
||||
|
||||
# Place.objects.get_list() returns all Places, regardless of whether they have
|
||||
# Restaurants.
|
||||
>>> Place.objects.get_list(order_by=['name'])
|
||||
>>> list(Place.objects.filter(order_by=['name']))
|
||||
[Ace Hardware the place, Demon Dogs the place]
|
||||
|
||||
>>> Restaurant.objects.get_object(place__id__exact=1)
|
||||
>>> Restaurant.objects.get(place__id__exact=1)
|
||||
Demon Dogs the restaurant
|
||||
>>> Restaurant.objects.get_object(pk=1)
|
||||
>>> Restaurant.objects.get(pk=1)
|
||||
Demon Dogs the restaurant
|
||||
>>> Restaurant.objects.get_object(place__exact=1)
|
||||
>>> Restaurant.objects.get(place__exact=1)
|
||||
Demon Dogs the restaurant
|
||||
>>> Restaurant.objects.get_object(place__pk=1)
|
||||
>>> Restaurant.objects.get(place__pk=1)
|
||||
Demon Dogs the restaurant
|
||||
>>> Restaurant.objects.get_object(place__name__startswith="Demon")
|
||||
>>> Restaurant.objects.get(place__name__startswith="Demon")
|
||||
Demon Dogs the restaurant
|
||||
|
||||
>>> Place.objects.get_object(id__exact=1)
|
||||
>>> Place.objects.get(id__exact=1)
|
||||
Demon Dogs the place
|
||||
>>> Place.objects.get_object(pk=1)
|
||||
>>> Place.objects.get(pk=1)
|
||||
Demon Dogs the place
|
||||
>>> Place.objects.get_object(restaurant__place__exact=1)
|
||||
>>> Place.objects.get(restaurant__place__exact=1)
|
||||
Demon Dogs the place
|
||||
>>> Place.objects.get_object(restaurant__pk=1)
|
||||
>>> Place.objects.get(restaurant__pk=1)
|
||||
Demon Dogs the place
|
||||
|
||||
# Add a Waiter to the Restaurant.
|
||||
>>> w = r.add_waiter(name='Joe')
|
||||
>>> w = r.waiter_set.add(name='Joe')
|
||||
>>> w.save()
|
||||
>>> w
|
||||
Joe the waiter at Demon Dogs the restaurant
|
||||
|
||||
# Query the waiters
|
||||
>>> Waiter.objects.get_list(restaurant__place__exact=1)
|
||||
>>> list(Waiter.objects.filter(restaurant__place__exact=1))
|
||||
[Joe the waiter at Demon Dogs the restaurant]
|
||||
>>> Waiter.objects.get_list(restaurant__pk=1)
|
||||
>>> list(Waiter.objects.filter(restaurant__pk=1))
|
||||
[Joe the waiter at Demon Dogs the restaurant]
|
||||
>>> Waiter.objects.get_list(id__exact=1)
|
||||
>>> list(Waiter.objects.filter(id__exact=1))
|
||||
[Joe the waiter at Demon Dogs the restaurant]
|
||||
>>> Waiter.objects.get_list(pk=1)
|
||||
>>> list(Waiter.objects.filter(pk=1))
|
||||
[Joe the waiter at Demon Dogs the restaurant]
|
||||
|
||||
# Delete the restaurant; the waiter should also be removed
|
||||
>>> r = Restaurant.objects.get_object(pk=1)
|
||||
>>> r = Restaurant.objects.get(pk=1)
|
||||
>>> r.delete()
|
||||
"""
|
||||
|
|
|
@ -38,26 +38,26 @@ API_TESTS = """
|
|||
|
||||
# By default, articles.get_list() orders by pub_date descending, then
|
||||
# headline ascending.
|
||||
>>> Article.objects.get_list()
|
||||
>>> list(Article.objects)
|
||||
[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.
|
||||
>>> Article.objects.get_list(order_by=['headline'])
|
||||
>>> list(Article.objects.order_by('headline'))
|
||||
[Article 1, Article 2, Article 3, Article 4]
|
||||
>>> Article.objects.get_list(order_by=['pub_date', '-headline'])
|
||||
>>> list(Article.objects.order_by('pub_date', '-headline')
|
||||
[Article 1, Article 3, Article 2, Article 4]
|
||||
|
||||
# Use the "limit" parameter to limit the results.
|
||||
>>> Article.objects.get_list(order_by=['headline'], limit=2)
|
||||
>>> list(Article.objects.order_by('headline')[:3])
|
||||
[Article 1, Article 2]
|
||||
|
||||
# Use the "offset" parameter with "limit" to offset the result list.
|
||||
>>> Article.objects.get_list(order_by=['headline'], offset=1, limit=2)
|
||||
>>> list(Article.objects.order_by('headline')[1:3])
|
||||
[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.
|
||||
>>> Article.objects.get_list(order_by=['?'])
|
||||
>>> list(Article.objects.order_by('?'))
|
||||
[...]
|
||||
"""
|
||||
|
|
|
@ -38,19 +38,19 @@ a
|
|||
>>> print u.when
|
||||
h
|
||||
|
||||
>>> Thing.objects.get_list(order_by=['when'])
|
||||
>>> list(Thing.objects.order_by('when'))
|
||||
[a, h]
|
||||
>>> v = Thing.objects.get_object(pk='a')
|
||||
>>> v = Thing.objects.get(pk='a')
|
||||
>>> print v.join
|
||||
b
|
||||
>>> print v.where
|
||||
2005-01-01
|
||||
>>> Thing.objects.get_list(order_by=['select.when'])
|
||||
>>> list(Thing.objects.order_by('select.when'))
|
||||
[a, h]
|
||||
|
||||
>>> Thing.objects.get_where_list('year')
|
||||
[datetime.datetime(2005, 1, 1, 0, 0), datetime.datetime(2006, 1, 1, 0, 0)]
|
||||
|
||||
>>> Thing.objects.get_list(where__month=1)
|
||||
>>> list(Thing.objects.filter(where__month=1))
|
||||
[a]
|
||||
"""
|
||||
|
|
|
@ -30,13 +30,13 @@ API_TESTS = """
|
|||
Before save
|
||||
After save
|
||||
|
||||
>>> Person.objects.get_list()
|
||||
>>> list(Person.objects)
|
||||
[John Smith]
|
||||
|
||||
>>> p1.delete()
|
||||
Before deletion
|
||||
After deletion
|
||||
|
||||
>>> Person.objects.get_list()
|
||||
>>> list(Person.objects)
|
||||
[]
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue