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