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:
parent
a6cf387d6b
commit
9fa5c43d5b
|
@ -13,7 +13,7 @@ class Article(models.Model):
|
|||
API_TESTS = """
|
||||
|
||||
# No articles are in the system yet.
|
||||
>>> list(Article.objects.all())
|
||||
>>> Article.objects.all()
|
||||
[]
|
||||
|
||||
# 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
|
||||
# is represented by "<Article object>", because we haven't given the Article
|
||||
# model a __repr__() method.
|
||||
>>> list(Article.objects.all())
|
||||
>>> Article.objects.all()
|
||||
[<Article object>]
|
||||
|
||||
# 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 object>
|
||||
|
||||
>>> list(Article.objects.filter(pub_date__year=2005))
|
||||
>>> Article.objects.filter(pub_date__year=2005)
|
||||
[<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>]
|
||||
|
||||
# Django raises an ArticleDoesNotExist exception for get()
|
||||
|
|
|
@ -23,16 +23,16 @@ API_TESTS = """
|
|||
>>> p.id
|
||||
1
|
||||
|
||||
>>> list(Person.objects.all())
|
||||
>>> Person.objects.all()
|
||||
[John Smith]
|
||||
|
||||
>>> list(Person.objects.filter(first_name__exact='John'))
|
||||
>>> Person.objects.filter(first_name__exact='John')
|
||||
[John Smith]
|
||||
|
||||
>>> Person.objects.get(first_name__exact='John')
|
||||
John Smith
|
||||
|
||||
>>> list(Person.objects.filter(firstname__exact='John'))
|
||||
>>> Person.objects.filter(firstname__exact='John')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: Cannot resolve keyword 'firstname' into field
|
||||
|
|
|
@ -8,7 +8,7 @@ from django.db import models
|
|||
|
||||
class PersonManager(models.Manager):
|
||||
def get_fun_people(self):
|
||||
return list(self.filter(fun=True))
|
||||
return self.filter(fun=True)
|
||||
|
||||
class Person(models.Model):
|
||||
first_name = models.CharField(maxlength=30)
|
||||
|
@ -68,20 +68,20 @@ Traceback (most recent call last):
|
|||
...
|
||||
AttributeError: type object 'Book' has no attribute 'objects'
|
||||
|
||||
>>> list(Book.published_objects)
|
||||
>>> Book.published_objects.all()
|
||||
[How to program]
|
||||
|
||||
>>> c1 = Car(name='Corvette', mileage=21, top_speed=180)
|
||||
>>> c1.save()
|
||||
>>> c2 = Car(name='Neon', mileage=31, top_speed=100)
|
||||
>>> c2.save()
|
||||
>>> list(Car.cars.order_by('name'))
|
||||
>>> Car.cars.order_by('name')
|
||||
[Corvette, Neon]
|
||||
>>> list(Car.fast_cars)
|
||||
>>> Car.fast_cars.all()
|
||||
[Corvette]
|
||||
|
||||
# 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".
|
||||
>>> list(Car._default_manager.order_by('name'))
|
||||
>>> Car._default_manager.order_by('name')
|
||||
[Corvette, Neon]
|
||||
"""
|
||||
|
|
|
@ -18,7 +18,7 @@ class Article(models.Model):
|
|||
return self.pub_date == datetime.date.today()
|
||||
|
||||
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):
|
||||
"""
|
||||
|
|
|
@ -29,12 +29,12 @@ class Business(models.Model):
|
|||
API_TESTS = """
|
||||
>>> dan = Employee(employee_code='ABC123', first_name='Dan', last_name='Jones')
|
||||
>>> dan.save()
|
||||
>>> list(Employee.objects.all())
|
||||
>>> Employee.objects.all()
|
||||
[Dan Jones]
|
||||
|
||||
>>> fran = Employee(employee_code='XYZ456', first_name='Fran', last_name='Bones')
|
||||
>>> fran.save()
|
||||
>>> list(Employee.objects.all())
|
||||
>>> Employee.objects.all()
|
||||
[Fran Bones, Dan Jones]
|
||||
|
||||
>>> Employee.objects.get(pk='ABC123')
|
||||
|
@ -54,7 +54,7 @@ Dan Jones
|
|||
>>> fran = Employee.objects.get(pk='XYZ456')
|
||||
>>> fran.last_name = 'Jones'
|
||||
>>> fran.save()
|
||||
>>> list(Employee.objects.filter(last_name__exact='Jones'))
|
||||
>>> Employee.objects.filter(last_name__exact='Jones')
|
||||
[Dan Jones, Fran Jones]
|
||||
>>> Employee.objects.in_bulk(['ABC123', 'XYZ456'])
|
||||
{'XYZ456': Fran Jones, 'ABC123': Dan Jones}
|
||||
|
@ -63,29 +63,29 @@ Dan Jones
|
|||
>>> b.save()
|
||||
>>> b.set_employees([dan.employee_code, fran.employee_code])
|
||||
True
|
||||
>>> list(b.employee_set)
|
||||
>>> b.employee_set.all()
|
||||
[Dan Jones, Fran Jones]
|
||||
>>> list(fran.business_set)
|
||||
>>> fran.business_set.all()
|
||||
[Sears]
|
||||
>>> Business.objects.in_bulk(['Sears'])
|
||||
{'Sears': Sears}
|
||||
|
||||
>>> list(Business.objects.filter(name__exact='Sears'))
|
||||
>>> Business.objects.filter(name__exact='Sears')
|
||||
[Sears]
|
||||
>>> list(Business.objects.filter(pk='Sears'))
|
||||
>>> Business.objects.filter(pk='Sears')
|
||||
[Sears]
|
||||
|
||||
# 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]
|
||||
>>> list(Employee.objects.filter(business__pk='Sears'))
|
||||
>>> Employee.objects.filter(business__pk='Sears')
|
||||
[Dan Jones, Fran Jones]
|
||||
|
||||
>>> list(Business.objects.filter(employees__employee_code__exact='ABC123'))
|
||||
>>> Business.objects.filter(employees__employee_code__exact='ABC123')
|
||||
[Sears]
|
||||
>>> list(Business.objects.filter(employees__pk='ABC123'))
|
||||
>>> Business.objects.filter(employees__pk='ABC123')
|
||||
[Sears]
|
||||
>>> list(Business.objects.filter(employees__first_name__startswith='Fran'))
|
||||
>>> Business.objects.filter(employees__first_name__startswith='Fran')
|
||||
[Sears]
|
||||
|
||||
"""
|
||||
|
|
|
@ -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
|
||||
# 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'}]
|
||||
>>> 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}]
|
||||
>>> 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
|
||||
|
@ -141,14 +141,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()
|
||||
>>> 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]
|
||||
>>> list(Article.objects.filter(headline__startswith='Article_'))
|
||||
>>> Article.objects.filter(headline__startswith='Article_')
|
||||
[Article_ with underscore]
|
||||
>>> a9 = Article(headline='Article% with percent sign', pub_date=datetime(2005, 11, 21))
|
||||
>>> 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]
|
||||
>>> list(Article.objects.filter(headline__startswith='Article%'))
|
||||
>>> Article.objects.filter(headline__startswith='Article%')
|
||||
[Article% with percent sign]
|
||||
"""
|
||||
|
|
|
@ -53,7 +53,7 @@ API_TESTS = """
|
|||
>>> w2.save()
|
||||
|
||||
# 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)]
|
||||
>>> w1.reporter
|
||||
John Smith
|
||||
|
@ -63,6 +63,6 @@ Jane Doe
|
|||
This is a test
|
||||
>>> w2.article
|
||||
This is a test
|
||||
>>> list(r1.writer_set)
|
||||
>>> r1.writer_set.all()
|
||||
[John Smith (Main writer)]
|
||||
"""
|
||||
|
|
|
@ -63,36 +63,32 @@ 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.
|
||||
>>> list(a1.primary_category_set)
|
||||
>>> a1.primary_category_set.all()
|
||||
[Crime, News]
|
||||
|
||||
# Ditto for the "primary_category" here.
|
||||
>>> list(a2.primary_category_set)
|
||||
>>> a2.primary_category_set.all()
|
||||
[News, Sports]
|
||||
|
||||
# Ditto for the "secondary_category" here.
|
||||
>>> list(a1.secondary_category_set)
|
||||
[Life]
|
||||
|
||||
# Ditto for the "secondary_category" here.
|
||||
>>> list(a2.secondary_category)
|
||||
>>> a1.secondary_category_set.all()
|
||||
[Life]
|
||||
|
||||
|
||||
>>> list(c1.primary_article_set)
|
||||
>>> c1.primary_article_set.all()
|
||||
[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]
|
||||
>>> list(c2.secondary_article_set)
|
||||
>>> c2.secondary_article_set.all()
|
||||
[]
|
||||
>>> list(c3.primary_article_set)
|
||||
>>> c3.primary_article_set.all()
|
||||
[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]
|
||||
"""
|
||||
|
|
|
@ -26,7 +26,7 @@ API_TESTS = """
|
|||
>>> c = Category(id=None, name='Child category', parent=r)
|
||||
>>> c.save()
|
||||
|
||||
>>> list(r.child_set)
|
||||
>>> r.child_set.all()
|
||||
[Child category]
|
||||
>>> r.child_set.get(name__startswith='Child')
|
||||
Child category
|
||||
|
@ -35,7 +35,7 @@ Traceback (most recent call last):
|
|||
...
|
||||
DoesNotExist
|
||||
|
||||
>>> list(c.child_set)
|
||||
>>> c.child_set.all()
|
||||
[]
|
||||
>>> c.parent
|
||||
Root category
|
||||
|
|
|
@ -32,12 +32,12 @@ API_TESTS = """
|
|||
Jane Smith
|
||||
>>> kid.father
|
||||
John Smith Senior
|
||||
>>> list(dad.fathers_child_set)
|
||||
>>> dad.fathers_child_set.all()
|
||||
[John Smith Junior]
|
||||
>>> list(mom.mothers_child_set)
|
||||
>>> mom.mothers_child_set.all()
|
||||
[John Smith Junior]
|
||||
>>> list(kid.mothers_child_set)
|
||||
>>> kid.mothers_child_set.all()
|
||||
[]
|
||||
>>> list(kid.fathers_child_set)
|
||||
>>> kid.fathers_child_set.all()
|
||||
[]
|
||||
"""
|
||||
|
|
|
@ -32,7 +32,7 @@ API_TESTS = """
|
|||
>>> m1 = man.save(data)
|
||||
|
||||
# Verify it worked.
|
||||
>>> list(Musician.objects.all())
|
||||
>>> Musician.objects.all()
|
||||
[Ella Fitzgerald]
|
||||
>>> [m1] == list(Musician.objects.all())
|
||||
True
|
||||
|
@ -66,7 +66,7 @@ True
|
|||
>>> a1 = man.save(data)
|
||||
|
||||
# Verify it worked.
|
||||
>>> list(Album.objects.all())
|
||||
>>> Album.objects.all()
|
||||
[Ella and Basie]
|
||||
>>> Album.objects.get().musician
|
||||
Ella Fitzgerald
|
||||
|
@ -79,7 +79,7 @@ Ella Fitzgerald
|
|||
>>> a2 = man.save(data)
|
||||
|
||||
# Verify it worked.
|
||||
>>> list(Album.objects.order_by('name'))
|
||||
>>> Album.objects.order_by('name')
|
||||
[Ella and Basie, Ultimate Ella]
|
||||
>>> a2 = Album.objects.get(pk=2)
|
||||
>>> a2
|
||||
|
|
|
@ -56,56 +56,56 @@ True
|
|||
True
|
||||
|
||||
# Article objects have access to their related Publication objects.
|
||||
>>> list(a1.publication_set)
|
||||
>>> a1.publication_set.all()
|
||||
[The Python Journal]
|
||||
>>> list(a2.publication_set)
|
||||
>>> a2.publication_set.all()
|
||||
[The Python Journal, Science News, Science Weekly]
|
||||
|
||||
# Publication objects have access to their related Article objects.
|
||||
>>> list(p2.article_set)
|
||||
>>> p2.article_set.all()
|
||||
[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]
|
||||
|
||||
# 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]
|
||||
>>> list(Article.objects.filter(publications__pk=1))
|
||||
>>> Article.objects.filter(publications__pk=1)
|
||||
[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]
|
||||
|
||||
>>> list(Article.objects.filter(publications__title__startswith="Science").distinct())
|
||||
>>> Article.objects.filter(publications__title__startswith="Science").distinct()
|
||||
[NASA uses Python]
|
||||
|
||||
# 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]
|
||||
>>> list(Publication.objects.filter(pk=1))
|
||||
>>> Publication.objects.filter(pk=1)
|
||||
[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]
|
||||
|
||||
>>> list(Publication.objects.filter(article__id__exact=1))
|
||||
>>> Publication.objects.filter(article__id__exact=1)
|
||||
[The Python Journal]
|
||||
|
||||
>>> list(Publication.objects.filter(article__pk=1))
|
||||
>>> 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()
|
||||
>>> list(Publication.objects.all())
|
||||
>>> Publication.objects.all()
|
||||
[Science News, Science Weekly]
|
||||
>>> 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.
|
||||
>>> a2.delete()
|
||||
>>> list(Article.objects.all())
|
||||
>>> Article.objects.all()
|
||||
[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]
|
||||
"""
|
||||
|
|
|
@ -59,10 +59,10 @@ John's second story
|
|||
2
|
||||
|
||||
# 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]
|
||||
|
||||
>>> list(r.article_set.filter(headline__startswith='This'))
|
||||
>>> r.article_set.filter(headline__startswith='This')
|
||||
This is a test
|
||||
|
||||
>>> r.article_set.count()
|
||||
|
@ -72,24 +72,24 @@ This is a test
|
|||
1
|
||||
|
||||
# Get articles by id
|
||||
>>> list(Article.objects.filter(id__exact=1))
|
||||
>>> Article.objects.filter(id__exact=1)
|
||||
[This is a test]
|
||||
>>> list(Article.objects.filter(pk=1))
|
||||
>>> Article.objects.filter(pk=1)
|
||||
[This is a test]
|
||||
|
||||
# Query on an article property
|
||||
>>> list(Article.objects.filter(headline__startswith='This'))
|
||||
>>> 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".
|
||||
>>> 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]
|
||||
|
||||
# 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]
|
||||
|
||||
# The underlying query only makes one join when a related table is referenced twice.
|
||||
|
@ -99,29 +99,29 @@ This is a test
|
|||
1
|
||||
|
||||
# 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]
|
||||
|
||||
# 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]
|
||||
>>> 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]
|
||||
|
||||
# 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):
|
||||
...
|
||||
TypeError: Cannot resolve keyword 'reporter_id' into field
|
||||
|
||||
# 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):
|
||||
...
|
||||
TypeError: Cannot resolve keyword 'reporter_id' into field
|
||||
|
||||
# "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]
|
||||
|
||||
# You can also instantiate an Article by passing
|
||||
|
@ -140,27 +140,27 @@ John Smith
|
|||
John Smith
|
||||
|
||||
# Reporters can be queried
|
||||
>>> list(Reporter.objects.filter(id__exact=1))
|
||||
>>> Reporter.objects.filter(id__exact=1)
|
||||
[John Smith]
|
||||
>>> list(Reporter.objects.filter(pk=1))
|
||||
>>> Reporter.objects.filter(pk=1)
|
||||
[John Smith]
|
||||
>>> list(Reporter.objects.filter(first_name__startswith='John'))
|
||||
>>> Reporter.objects.filter(first_name__startswith='John')
|
||||
[John Smith]
|
||||
|
||||
# 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]
|
||||
>>> list(Reporter.objects.filter(article__pk=1))
|
||||
>>> Reporter.objects.filter(article__pk=1)
|
||||
[John Smith]
|
||||
>>> list(Reporter.objects.filter(article__headline__startswith='This'))
|
||||
>>> Reporter.objects.filter(article__headline__startswith='This')
|
||||
[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]
|
||||
|
||||
# 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]
|
||||
>>> list(Reporter.objects.filter(article__reporter__first_name__startswith='John').distinct())
|
||||
>>> Reporter.objects.filter(article__reporter__first_name__startswith='John').distinct()
|
||||
[John Smith]
|
||||
|
||||
# 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
|
||||
|
||||
# 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]
|
||||
>>> list(Reporter.objects.order_by('first_name'))
|
||||
>>> Reporter.objects.order_by('first_name')
|
||||
[John Smith, Paul Jones]
|
||||
>>> r.delete()
|
||||
>>> list(Article.objects.order_by('headline'))
|
||||
>>> Article.objects.order_by('headline')
|
||||
[Paul's story]
|
||||
>>> list(Reporter.objects.order_by('first_name'))
|
||||
>>> Reporter.objects.order_by('first_name')
|
||||
[Paul Jones]
|
||||
|
||||
"""
|
||||
|
|
|
@ -46,9 +46,9 @@ Second
|
|||
1
|
||||
|
||||
# Reporter objects have access to their related Article objects.
|
||||
>>> list(r.article_set.order_by('headline'))
|
||||
>>> r.article_set.order_by('headline')
|
||||
[First, Second]
|
||||
>>> list(r.article_set.filter(headline__startswith='Fir'))
|
||||
>>> r.article_set.filter(headline__startswith='Fir')
|
||||
First
|
||||
>>> r.article_set.count()
|
||||
2
|
||||
|
@ -73,6 +73,6 @@ Traceback (most recent call last):
|
|||
DoesNotExist
|
||||
|
||||
# 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]
|
||||
"""
|
||||
|
|
|
@ -55,13 +55,13 @@ 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.
|
||||
>>> list(Restaurant.objects.all())
|
||||
# Restaurant.objects.all() just returns the Restaurants, not the Places.
|
||||
>>> Restaurant.objects.all()
|
||||
[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.
|
||||
>>> list(Place.objects.order_by('name'))
|
||||
>>> Place.objects.order_by('name')
|
||||
[Ace Hardware the place, Demon Dogs the place]
|
||||
|
||||
>>> Restaurant.objects.get(place__id__exact=1)
|
||||
|
@ -91,13 +91,13 @@ Demon Dogs the place
|
|||
Joe the waiter at Demon Dogs the restaurant
|
||||
|
||||
# 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]
|
||||
>>> list(Waiter.objects.filter(restaurant__pk=1))
|
||||
>>> Waiter.objects.filter(restaurant__pk=1)
|
||||
[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]
|
||||
>>> list(Waiter.objects.filter(pk=1))
|
||||
>>> Waiter.objects.filter(pk=1)
|
||||
[Joe the waiter at Demon Dogs the restaurant]
|
||||
|
||||
# Delete the restaurant; the waiter should also be removed
|
||||
|
|
|
@ -38,26 +38,26 @@ API_TESTS = """
|
|||
|
||||
# By default, Article.objects.all() orders by pub_date descending, then
|
||||
# headline ascending.
|
||||
>>> list(Article.objects.all())
|
||||
>>> Article.objects.all()
|
||||
[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.
|
||||
>>> list(Article.objects.order_by('headline'))
|
||||
>>> Article.objects.order_by('headline')
|
||||
[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]
|
||||
|
||||
# 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]
|
||||
|
||||
# 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]
|
||||
|
||||
# Use '?' to order randomly. (We're using [...] in the output to indicate we
|
||||
# don't know what order the output will be in.
|
||||
>>> list(Article.objects.order_by('?'))
|
||||
>>> Article.objects.order_by('?')
|
||||
[...]
|
||||
"""
|
||||
|
|
|
@ -38,19 +38,19 @@ a
|
|||
>>> print u.when
|
||||
h
|
||||
|
||||
>>> list(Thing.objects.order_by('when'))
|
||||
>>> Thing.objects.order_by('when')
|
||||
[a, h]
|
||||
>>> v = Thing.objects.get(pk='a')
|
||||
>>> print v.join
|
||||
b
|
||||
>>> print v.where
|
||||
2005-01-01
|
||||
>>> list(Thing.objects.order_by('select.when'))
|
||||
>>> Thing.objects.order_by('select.when')
|
||||
[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)]
|
||||
|
||||
>>> list(Thing.objects.filter(where__month=1))
|
||||
>>> Thing.objects.filter(where__month=1)
|
||||
[a]
|
||||
"""
|
||||
|
|
|
@ -30,13 +30,13 @@ API_TESTS = """
|
|||
Before save
|
||||
After save
|
||||
|
||||
>>> list(Person.objects.all())
|
||||
>>> Person.objects.all()
|
||||
[John Smith]
|
||||
|
||||
>>> p1.delete()
|
||||
Before deletion
|
||||
After deletion
|
||||
|
||||
>>> list(Person.objects.all())
|
||||
>>> Person.objects.all()
|
||||
[]
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue