1
0
mirror of https://github.com/django/django.git synced 2025-06-05 03:29:12 +00:00

magic-removal: Changed syntax in unit tests -- hopefully for the last time.

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2195 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2006-01-31 01:08:02 +00:00
parent a6cf387d6b
commit 9fa5c43d5b
18 changed files with 120 additions and 124 deletions

View File

@ -13,7 +13,7 @@ class Article(models.Model):
API_TESTS = """ API_TESTS = """
# No articles are in the system yet. # No articles are in the system yet.
>>> list(Article.objects.all()) >>> Article.objects.all()
[] []
# Create an Article. # Create an Article.
@ -40,7 +40,7 @@ datetime.datetime(2005, 7, 28, 0, 0)
# Listing objects displays all the articles in the database. Note that the article # Listing objects displays all the articles in the database. Note that the article
# is represented by "<Article object>", because we haven't given the Article # is represented by "<Article object>", because we haven't given the Article
# model a __repr__() method. # model a __repr__() method.
>>> list(Article.objects.all()) >>> Article.objects.all()
[<Article object>] [<Article object>]
# Django provides a rich database lookup API that's entirely driven by # Django provides a rich database lookup API that's entirely driven by
@ -62,11 +62,11 @@ datetime.datetime(2005, 7, 28, 0, 0)
>>> Article.objects.get(headline='Area woman programs in Python') >>> Article.objects.get(headline='Area woman programs in Python')
<Article object> <Article object>
>>> list(Article.objects.filter(pub_date__year=2005)) >>> Article.objects.filter(pub_date__year=2005)
[<Article object>] [<Article object>]
>>> list(Article.objects.filter(pub_date__year=2004)) >>> Article.objects.filter(pub_date__year=2004)
[] []
>>> list(Article.objects.filter(pub_date__year=2005, pub_date__month=7)) >>> Article.objects.filter(pub_date__year=2005, pub_date__month=7)
[<Article object>] [<Article object>]
# Django raises an ArticleDoesNotExist exception for get() # Django raises an ArticleDoesNotExist exception for get()

View File

@ -23,16 +23,16 @@ API_TESTS = """
>>> p.id >>> p.id
1 1
>>> list(Person.objects.all()) >>> Person.objects.all()
[John Smith] [John Smith]
>>> list(Person.objects.filter(first_name__exact='John')) >>> Person.objects.filter(first_name__exact='John')
[John Smith] [John Smith]
>>> Person.objects.get(first_name__exact='John') >>> Person.objects.get(first_name__exact='John')
John Smith John Smith
>>> list(Person.objects.filter(firstname__exact='John')) >>> Person.objects.filter(firstname__exact='John')
Traceback (most recent call last): Traceback (most recent call last):
... ...
TypeError: Cannot resolve keyword 'firstname' into field TypeError: Cannot resolve keyword 'firstname' into field

View File

@ -8,7 +8,7 @@ from django.db import models
class PersonManager(models.Manager): class PersonManager(models.Manager):
def get_fun_people(self): def get_fun_people(self):
return list(self.filter(fun=True)) return self.filter(fun=True)
class Person(models.Model): class Person(models.Model):
first_name = models.CharField(maxlength=30) first_name = models.CharField(maxlength=30)
@ -68,20 +68,20 @@ Traceback (most recent call last):
... ...
AttributeError: type object 'Book' has no attribute 'objects' AttributeError: type object 'Book' has no attribute 'objects'
>>> list(Book.published_objects) >>> Book.published_objects.all()
[How to program] [How to program]
>>> c1 = Car(name='Corvette', mileage=21, top_speed=180) >>> c1 = Car(name='Corvette', mileage=21, top_speed=180)
>>> c1.save() >>> c1.save()
>>> c2 = Car(name='Neon', mileage=31, top_speed=100) >>> c2 = Car(name='Neon', mileage=31, top_speed=100)
>>> c2.save() >>> c2.save()
>>> list(Car.cars.order_by('name')) >>> Car.cars.order_by('name')
[Corvette, Neon] [Corvette, Neon]
>>> list(Car.fast_cars) >>> Car.fast_cars.all()
[Corvette] [Corvette]
# Each model class gets a "_default_manager" attribute, which is a reference # Each model class gets a "_default_manager" attribute, which is a reference
# to the first manager defined in the class. In this case, it's "cars". # to the first manager defined in the class. In this case, it's "cars".
>>> list(Car._default_manager.order_by('name')) >>> Car._default_manager.order_by('name')
[Corvette, Neon] [Corvette, Neon]
""" """

View File

@ -18,7 +18,7 @@ class Article(models.Model):
return self.pub_date == datetime.date.today() return self.pub_date == datetime.date.today()
def get_articles_from_same_day_1(self): def get_articles_from_same_day_1(self):
return Article.objects.get_list(id__ne=self.id, pub_date__exact=self.pub_date) return Article.objects.filter(id__ne=self.id, pub_date__exact=self.pub_date)
def get_articles_from_same_day_2(self): def get_articles_from_same_day_2(self):
""" """

View File

@ -29,12 +29,12 @@ 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()
>>> list(Employee.objects.all()) >>> Employee.objects.all()
[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()
>>> list(Employee.objects.all()) >>> Employee.objects.all()
[Fran Bones, Dan Jones] [Fran Bones, Dan Jones]
>>> Employee.objects.get(pk='ABC123') >>> Employee.objects.get(pk='ABC123')
@ -54,7 +54,7 @@ Dan Jones
>>> fran = Employee.objects.get(pk='XYZ456') >>> fran = Employee.objects.get(pk='XYZ456')
>>> fran.last_name = 'Jones' >>> fran.last_name = 'Jones'
>>> fran.save() >>> fran.save()
>>> list(Employee.objects.filter(last_name__exact='Jones')) >>> Employee.objects.filter(last_name__exact='Jones')
[Dan Jones, Fran Jones] [Dan Jones, Fran Jones]
>>> Employee.objects.in_bulk(['ABC123', 'XYZ456']) >>> Employee.objects.in_bulk(['ABC123', 'XYZ456'])
{'XYZ456': Fran Jones, 'ABC123': Dan Jones} {'XYZ456': Fran Jones, 'ABC123': Dan Jones}
@ -63,29 +63,29 @@ Dan Jones
>>> b.save() >>> b.save()
>>> b.set_employees([dan.employee_code, fran.employee_code]) >>> b.set_employees([dan.employee_code, fran.employee_code])
True True
>>> list(b.employee_set) >>> b.employee_set.all()
[Dan Jones, Fran Jones] [Dan Jones, Fran Jones]
>>> list(fran.business_set) >>> fran.business_set.all()
[Sears] [Sears]
>>> Business.objects.in_bulk(['Sears']) >>> Business.objects.in_bulk(['Sears'])
{'Sears': Sears} {'Sears': Sears}
>>> list(Business.objects.filter(name__exact='Sears')) >>> Business.objects.filter(name__exact='Sears')
[Sears] [Sears]
>>> list(Business.objects.filter(pk='Sears')) >>> Business.objects.filter(pk='Sears')
[Sears] [Sears]
# Queries across tables, involving primary key # Queries across tables, involving primary key
>>> list(Employee.objects.filter(business__name__exact='Sears')) >>> Employee.objects.filter(business__name__exact='Sears')
[Dan Jones, Fran Jones] [Dan Jones, Fran Jones]
>>> list(Employee.objects.filter(business__pk='Sears')) >>> Employee.objects.filter(business__pk='Sears')
[Dan Jones, Fran Jones] [Dan Jones, Fran Jones]
>>> list(Business.objects.filter(employees__employee_code__exact='ABC123')) >>> Business.objects.filter(employees__employee_code__exact='ABC123')
[Sears] [Sears]
>>> list(Business.objects.filter(employees__pk='ABC123')) >>> Business.objects.filter(employees__pk='ABC123')
[Sears] [Sears]
>>> list(Business.objects.filter(employees__first_name__startswith='Fran')) >>> Business.objects.filter(employees__first_name__startswith='Fran')
[Sears] [Sears]
""" """

View File

@ -84,9 +84,9 @@ TypeError: in_bulk() got an unexpected keyword argument 'headline__startswith'
# values() returns a list of dictionaries instead of object instances -- and # values() returns a list of dictionaries instead of object instances -- and
# you can specify which fields you want to retrieve. # you can specify which fields you want to retrieve.
>>> list(Article.objects.values('headline')) >>> Article.objects.values('headline')
[{'headline': 'Article 5'}, {'headline': 'Article 6'}, {'headline': 'Article 4'}, {'headline': 'Article 2'}, {'headline': 'Article 3'}, {'headline': 'Article 7'}, {'headline': 'Article 1'}] [{'headline': 'Article 5'}, {'headline': 'Article 6'}, {'headline': 'Article 4'}, {'headline': 'Article 2'}, {'headline': 'Article 3'}, {'headline': 'Article 7'}, {'headline': 'Article 1'}]
>>> list(Article.objects.filter(pub_date__exact=datetime(2005, 7, 27)).values('id')) >>> Article.objects.filter(pub_date__exact=datetime(2005, 7, 27)).values('id')
[{'id': 2}, {'id': 3}, {'id': 7}] [{'id': 2}, {'id': 3}, {'id': 7}]
>>> list(Article.objects.values('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'}] >>> list(Article.objects.values('id', 'headline')) == [{'id': 5, 'headline': 'Article 5'}, {'id': 6, 'headline': 'Article 6'}, {'id': 4, 'headline': 'Article 4'}, {'id': 2, 'headline': 'Article 2'}, {'id': 3, 'headline': 'Article 3'}, {'id': 7, 'headline': 'Article 7'}, {'id': 1, 'headline': 'Article 1'}]
True True
@ -141,14 +141,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()
>>> list(Article.objects.filter(headline__startswith='Article')) >>> 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]
>>> list(Article.objects.filter(headline__startswith='Article_')) >>> 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()
>>> list(Article.objects.filter(headline__startswith='Article')) >>> 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]
>>> list(Article.objects.filter(headline__startswith='Article%')) >>> Article.objects.filter(headline__startswith='Article%')
[Article% with percent sign] [Article% with percent sign]
""" """

View File

@ -53,7 +53,7 @@ API_TESTS = """
>>> w2.save() >>> w2.save()
# Play around with the API. # Play around with the API.
>>> list(a.writer_set.order_by('-position').extra(select_related=True)) >>> a.writer_set.select_related().order_by('-position')
[John Smith (Main writer), Jane Doe (Contributor)] [John Smith (Main writer), Jane Doe (Contributor)]
>>> w1.reporter >>> w1.reporter
John Smith John Smith
@ -63,6 +63,6 @@ Jane Doe
This is a test This is a test
>>> w2.article >>> w2.article
This is a test This is a test
>>> list(r1.writer_set) >>> r1.writer_set.all()
[John Smith (Main writer)] [John Smith (Main writer)]
""" """

View File

@ -63,36 +63,32 @@ 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.
>>> list(a1.primary_category_set) >>> a1.primary_category_set.all()
[Crime, News] [Crime, News]
# Ditto for the "primary_category" here. # Ditto for the "primary_category" here.
>>> list(a2.primary_category_set) >>> a2.primary_category_set.all()
[News, Sports] [News, Sports]
# Ditto for the "secondary_category" here. # Ditto for the "secondary_category" here.
>>> list(a1.secondary_category_set) >>> a1.secondary_category_set.all()
[Life]
# Ditto for the "secondary_category" here.
>>> list(a2.secondary_category)
[Life] [Life]
>>> list(c1.primary_article_set) >>> c1.primary_article_set.all()
[Area man runs] [Area man runs]
>>> list(c1.secondary_article_set) >>> c1.secondary_article_set.all()
[] []
>>> list(c2.primary_article_set) >>> c2.primary_article_set.all()
[Area man steals, Area man runs] [Area man steals, Area man runs]
>>> list(c2.secondary_article_set) >>> c2.secondary_article_set.all()
[] []
>>> list(c3.primary_article_set) >>> c3.primary_article_set.all()
[Area man steals] [Area man steals]
>>> list(c3.secondary_article_set) >>> c3.secondary_article_set.all()
[] []
>>> list(c4.primary_article_set) >>> c4.primary_article_set.all()
[] []
>>> list(c4.secondary_article_set) >>> c4.secondary_article_set.all()
[Area man steals, Area man runs] [Area man steals, Area man runs]
""" """

View File

@ -26,7 +26,7 @@ API_TESTS = """
>>> c = Category(id=None, name='Child category', parent=r) >>> c = Category(id=None, name='Child category', parent=r)
>>> c.save() >>> c.save()
>>> list(r.child_set) >>> r.child_set.all()
[Child category] [Child category]
>>> r.child_set.get(name__startswith='Child') >>> r.child_set.get(name__startswith='Child')
Child category Child category
@ -35,7 +35,7 @@ Traceback (most recent call last):
... ...
DoesNotExist DoesNotExist
>>> list(c.child_set) >>> c.child_set.all()
[] []
>>> c.parent >>> c.parent
Root category Root category

View File

@ -32,12 +32,12 @@ API_TESTS = """
Jane Smith Jane Smith
>>> kid.father >>> kid.father
John Smith Senior John Smith Senior
>>> list(dad.fathers_child_set) >>> dad.fathers_child_set.all()
[John Smith Junior] [John Smith Junior]
>>> list(mom.mothers_child_set) >>> mom.mothers_child_set.all()
[John Smith Junior] [John Smith Junior]
>>> list(kid.mothers_child_set) >>> kid.mothers_child_set.all()
[] []
>>> list(kid.fathers_child_set) >>> kid.fathers_child_set.all()
[] []
""" """

View File

@ -32,7 +32,7 @@ API_TESTS = """
>>> m1 = man.save(data) >>> m1 = man.save(data)
# Verify it worked. # Verify it worked.
>>> list(Musician.objects.all()) >>> Musician.objects.all()
[Ella Fitzgerald] [Ella Fitzgerald]
>>> [m1] == list(Musician.objects.all()) >>> [m1] == list(Musician.objects.all())
True True
@ -66,7 +66,7 @@ True
>>> a1 = man.save(data) >>> a1 = man.save(data)
# Verify it worked. # Verify it worked.
>>> list(Album.objects.all()) >>> Album.objects.all()
[Ella and Basie] [Ella and Basie]
>>> Album.objects.get().musician >>> Album.objects.get().musician
Ella Fitzgerald Ella Fitzgerald
@ -79,7 +79,7 @@ Ella Fitzgerald
>>> a2 = man.save(data) >>> a2 = man.save(data)
# Verify it worked. # Verify it worked.
>>> list(Album.objects.order_by('name')) >>> Album.objects.order_by('name')
[Ella and Basie, Ultimate Ella] [Ella and Basie, Ultimate Ella]
>>> a2 = Album.objects.get(pk=2) >>> a2 = Album.objects.get(pk=2)
>>> a2 >>> a2

View File

@ -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.
>>> list(a1.publication_set) >>> a1.publication_set.all()
[The Python Journal] [The Python Journal]
>>> list(a2.publication_set) >>> a2.publication_set.all()
[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.
>>> list(p2.article_set) >>> p2.article_set.all()
[NASA uses Python] [NASA uses Python]
>>> list(p1.article_set.order_by('headline')) >>> 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
>>> list(Article.objects.filter(publications__id__exact=1)) >>> 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]
>>> list(Article.objects.filter(publications__pk=1)) >>> 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]
>>> list(Article.objects.filter(publications__title__startswith="Science")) >>> Article.objects.filter(publications__title__startswith="Science")
[NASA uses Python, NASA uses Python] [NASA uses Python, NASA uses Python]
>>> list(Article.objects.filter(publications__title__startswith="Science").distinct()) >>> Article.objects.filter(publications__title__startswith="Science").distinct()
[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)
>>> list(Publication.objects.filter(id__exact=1)) >>> Publication.objects.filter(id__exact=1)
[The Python Journal] [The Python Journal]
>>> list(Publication.objects.filter(pk=1)) >>> Publication.objects.filter(pk=1)
[The Python Journal] [The Python Journal]
>>> list(Publication.objects.filter(article__headline__startswith="NASA")) >>> Publication.objects.filter(article__headline__startswith="NASA")
[The Python Journal, Science News, Science Weekly] [The Python Journal, Science News, Science Weekly]
>>> list(Publication.objects.filter(article__id__exact=1)) >>> Publication.objects.filter(article__id__exact=1)
[The Python Journal] [The Python Journal]
>>> list(Publication.objects.filter(article__pk=1)) >>> 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()
>>> list(Publication.objects.all()) >>> Publication.objects.all()
[Science News, Science Weekly] [Science News, Science Weekly]
>>> a1 = Article.objects.get(pk=1) >>> a1 = Article.objects.get(pk=1)
>>> list(a1.publication_set) >>> a1.publication_set.all()
[] []
# 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()
>>> list(Article.objects.all()) >>> Article.objects.all()
[Django lets you build Web apps easily] [Django lets you build Web apps easily]
>>> list(p1.article_set.order_by('headline')) >>> p1.article_set.order_by('headline')
[Django lets you build Web apps easily] [Django lets you build Web apps easily]
""" """

View File

@ -59,10 +59,10 @@ John's second story
2 2
# Reporter objects have access to their related Article objects. # Reporter objects have access to their related Article objects.
>>> list(r.article_set.order_by('pub_date')) >>> r.article_set.order_by('pub_date')
[This is a test, John's second story] [This is a test, John's second story]
>>> list(r.article_set.filter(headline__startswith='This')) >>> r.article_set.filter(headline__startswith='This')
This is a test This is a test
>>> r.article_set.count() >>> r.article_set.count()
@ -72,24 +72,24 @@ This is a test
1 1
# Get articles by id # Get articles by id
>>> list(Article.objects.filter(id__exact=1)) >>> Article.objects.filter(id__exact=1)
[This is a test] [This is a test]
>>> list(Article.objects.filter(pk=1)) >>> Article.objects.filter(pk=1)
[This is a test] [This is a test]
# Query on an article property # Query on an article property
>>> list(Article.objects.filter(headline__startswith='This')) >>> 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".
>>> list(Article.objects.filter(reporter__first_name__exact='John').order_by('pub_date')) >>> 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.
>>> list(Article.objects.filter(reporter__first_name__exact='John', reporter__last_name__exact='Smith')) >>> 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.
@ -99,29 +99,29 @@ This is a test
1 1
# The automatically joined table has a predictable name. # The automatically joined table has a predictable name.
>>> list(Article.objects.filter(reporter__first_name__exact='John').extra(where=["many_to_one_article__reporter.last_name='Smith'"])) >>> 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.
>>> list(Article.objects.filter(reporter__id__exact=1).order_by('pub_date')) >>> 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]
>>> list(Article.objects.filter(reporter__pk=1).order_by('pub_date')) >>> 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.
>>> list(Article.objects.filter(reporter_id__exact=1)) >>> 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
>>> list(Article.objects.filter(reporter_id=1)) >>> Article.objects.filter(reporter_id=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
# "pk" shortcut syntax works in a related context, too. # "pk" shortcut syntax works in a related context, too.
>>> list(Article.objects.filter(reporter__pk=1).order_by('pub_date')) >>> 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
@ -140,27 +140,27 @@ John Smith
John Smith John Smith
# Reporters can be queried # Reporters can be queried
>>> list(Reporter.objects.filter(id__exact=1)) >>> Reporter.objects.filter(id__exact=1)
[John Smith] [John Smith]
>>> list(Reporter.objects.filter(pk=1)) >>> Reporter.objects.filter(pk=1)
[John Smith] [John Smith]
>>> list(Reporter.objects.filter(first_name__startswith='John')) >>> 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
>>> list(Reporter.objects.filter(article__id__exact=1)) >>> Reporter.objects.filter(article__id__exact=1)
[John Smith] [John Smith]
>>> list(Reporter.objects.filter(article__pk=1)) >>> Reporter.objects.filter(article__pk=1)
[John Smith] [John Smith]
>>> list(Reporter.objects.filter(article__headline__startswith='This')) >>> Reporter.objects.filter(article__headline__startswith='This')
[John Smith, John Smith, John Smith] [John Smith, John Smith, John Smith]
>>> list(Reporter.objects.filter(article__headline__startswith='This').distinct()) >>> Reporter.objects.filter(article__headline__startswith='This').distinct()
[John Smith] [John Smith]
# Queries can go round in circles. # Queries can go round in circles.
>>> list(Reporter.objects.filter(article__reporter__first_name__startswith='John')) >>> 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]
>>> list(Reporter.objects.filter(article__reporter__first_name__startswith='John').distinct()) >>> Reporter.objects.filter(article__reporter__first_name__startswith='John').distinct()
[John Smith] [John Smith]
# Deletes that require joins are prohibited. # Deletes that require joins are prohibited.
@ -170,14 +170,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.
>>> list(Article.objects.order_by('headline')) >>> 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]
>>> list(Reporter.objects.order_by('first_name')) >>> Reporter.objects.order_by('first_name')
[John Smith, Paul Jones] [John Smith, Paul Jones]
>>> r.delete() >>> r.delete()
>>> list(Article.objects.order_by('headline')) >>> Article.objects.order_by('headline')
[Paul's story] [Paul's story]
>>> list(Reporter.objects.order_by('first_name')) >>> Reporter.objects.order_by('first_name')
[Paul Jones] [Paul Jones]
""" """

View File

@ -46,9 +46,9 @@ Second
1 1
# Reporter objects have access to their related Article objects. # Reporter objects have access to their related Article objects.
>>> list(r.article_set.order_by('headline')) >>> r.article_set.order_by('headline')
[First, Second] [First, Second]
>>> list(r.article_set.filter(headline__startswith='Fir')) >>> r.article_set.filter(headline__startswith='Fir')
First First
>>> r.article_set.count() >>> r.article_set.count()
2 2
@ -73,6 +73,6 @@ 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".
>>> list(Article.objects.filter(reporter__isnull=True)) >>> Article.objects.filter(reporter__isnull=True)
[Third] [Third]
""" """

View File

@ -55,13 +55,13 @@ 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.all() just returns the Restaurants, not the Places.
>>> list(Restaurant.objects.all()) >>> Restaurant.objects.all()
[Demon Dogs the restaurant] [Demon Dogs the restaurant]
# Place.objects.get_list() returns all Places, regardless of whether they have # Place.objects.all() returns all Places, regardless of whether they have
# Restaurants. # Restaurants.
>>> list(Place.objects.order_by('name')) >>> Place.objects.order_by('name')
[Ace Hardware the place, Demon Dogs the place] [Ace Hardware the place, Demon Dogs the place]
>>> Restaurant.objects.get(place__id__exact=1) >>> Restaurant.objects.get(place__id__exact=1)
@ -91,13 +91,13 @@ Demon Dogs the place
Joe the waiter at Demon Dogs the restaurant Joe the waiter at Demon Dogs the restaurant
# Query the waiters # Query the waiters
>>> list(Waiter.objects.filter(restaurant__place__exact=1)) >>> Waiter.objects.filter(restaurant__place__exact=1)
[Joe the waiter at Demon Dogs the restaurant] [Joe the waiter at Demon Dogs the restaurant]
>>> list(Waiter.objects.filter(restaurant__pk=1)) >>> Waiter.objects.filter(restaurant__pk=1)
[Joe the waiter at Demon Dogs the restaurant] [Joe the waiter at Demon Dogs the restaurant]
>>> list(Waiter.objects.filter(id__exact=1)) >>> Waiter.objects.filter(id__exact=1)
[Joe the waiter at Demon Dogs the restaurant] [Joe the waiter at Demon Dogs the restaurant]
>>> list(Waiter.objects.filter(pk=1)) >>> 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

View File

@ -38,26 +38,26 @@ API_TESTS = """
# By default, Article.objects.all() orders by pub_date descending, then # By default, Article.objects.all() orders by pub_date descending, then
# headline ascending. # headline ascending.
>>> list(Article.objects.all()) >>> Article.objects.all()
[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.
>>> list(Article.objects.order_by('headline')) >>> Article.objects.order_by('headline')
[Article 1, Article 2, Article 3, Article 4] [Article 1, Article 2, Article 3, Article 4]
>>> list(Article.objects.order_by('pub_date', '-headline') >>> 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.
>>> list(Article.objects.order_by('headline')[:3]) >>> 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.
>>> list(Article.objects.order_by('headline')[1:3]) >>> 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.
>>> list(Article.objects.order_by('?')) >>> Article.objects.order_by('?')
[...] [...]
""" """

View File

@ -38,19 +38,19 @@ a
>>> print u.when >>> print u.when
h h
>>> list(Thing.objects.order_by('when')) >>> Thing.objects.order_by('when')
[a, h] [a, h]
>>> v = Thing.objects.get(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
>>> list(Thing.objects.order_by('select.when')) >>> Thing.objects.order_by('select.when')
[a, h] [a, h]
>>> Thing.objects.get_where_list('year') >>> Thing.objects.dates('where', '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)]
>>> list(Thing.objects.filter(where__month=1)) >>> Thing.objects.filter(where__month=1)
[a] [a]
""" """

View File

@ -30,13 +30,13 @@ API_TESTS = """
Before save Before save
After save After save
>>> list(Person.objects.all()) >>> Person.objects.all()
[John Smith] [John Smith]
>>> p1.delete() >>> p1.delete()
Before deletion Before deletion
After deletion After deletion
>>> list(Person.objects.all()) >>> Person.objects.all()
[] []
""" """