1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

magic-removal: Fixed #1186 -- Fixed problem resolving primary key in some 'pk' database queries. Also lightly refactored query-parsing code. Thanks, Russ

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@1856 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty
2006-01-08 06:16:05 +00:00
parent 2bc39d88e6
commit f998c7bcc6
7 changed files with 333 additions and 172 deletions

View File

@@ -35,7 +35,7 @@ John Smith
>>> Person.objects.get_list(firstname__exact='John')
Traceback (most recent call last):
...
TypeError: got unexpected keyword argument 'firstname__exact'
TypeError: Cannot resolve keyword 'firstname' into field
>>> p = Person.objects.get_object(last_name__exact='Smith')
>>> p.first_name

View File

@@ -47,6 +47,10 @@ Traceback (most recent call last):
...
DoesNotExist: Employee does not exist for {'pk': 'foo'}
# Use the name of the primary key, rather than pk.
>>> Employee.objects.get_object(employee_code__exact='ABC123')
Dan Jones
# Fran got married and changed her last name.
>>> fran = Employee.objects.get_object(pk='XYZ456')
>>> fran.last_name = 'Jones'
@@ -66,4 +70,23 @@ True
[Sears]
>>> Business.objects.get_in_bulk(['Sears'])
{'Sears': Sears}
>>> Business.objects.get_list(name__exact='Sears')
[Sears]
>>> Business.objects.get_list(pk='Sears')
[Sears]
# Queries across tables, involving primary key
>>> Employee.objects.get_list(businesses__name__exact='Sears')
[Dan Jones, Fran Jones]
>>> Employee.objects.get_list(businesses__pk='Sears')
[Dan Jones, Fran Jones]
>>> Business.objects.get_list(employees__employee_code__exact='ABC123')
[Sears]
>>> Business.objects.get_list(employees__pk='ABC123')
[Sears]
>>> Business.objects.get_list(employees__first_name__startswith='Fran')
[Sears]
"""

View File

@@ -68,6 +68,8 @@ True
[Django lets you build Web apps easily, NASA uses Python]
# We can perform kwarg queries across m2m relationships
>>> Article.objects.get_list(publications__id__exact=1)
[Django lets you build Web apps easily, NASA uses Python]
>>> Article.objects.get_list(publications__pk=1)
[Django lets you build Web apps easily, NASA uses Python]
@@ -78,9 +80,17 @@ True
[NASA uses Python]
# Reverse m2m queries (i.e., start at the table that doesn't have a ManyToManyField)
>>> Publication.objects.get_list(id__exact=1)
[The Python Journal]
>>> Publication.objects.get_list(pk=1)
[The Python Journal]
>>> Publication.objects.get_list(articles__headline__startswith="NASA")
[The Python Journal, Science News, Science Weekly]
>>> Publication.objects.get_list(articles__id__exact=1)
[The Python Journal]
>>> Publication.objects.get_list(articles__pk=1)
[The Python Journal]

View File

@@ -22,6 +22,7 @@ class Article(models.Model):
def __repr__(self):
return self.headline
API_TESTS = """
# Create a Reporter.
>>> r = Reporter(first_name='John', last_name='Smith', email='john@example.com')
@@ -60,6 +61,16 @@ This is a test
>>> r.get_article_count()
2
# Get articles by id
>>> Article.objects.get_list(id__exact=1)
[This is a test]
>>> Article.objects.get_list(pk=1)
[This is a test]
# Query on an article property
>>> Article.objects.get_list(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.
@@ -83,12 +94,20 @@ This is a test
# Find all Articles for the Reporter whose ID is 1.
>>> Article.objects.get_list(reporter__id__exact=1, order_by=['pub_date'])
[This is a test, John's second story]
>>> Article.objects.get_list(reporter__pk=1, order_by=['pub_date'])
[This is a test, John's second story]
# Note 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)
Traceback (most recent call last):
...
TypeError: got unexpected keyword argument 'reporter_id__exact'
TypeError: Cannot resolve keyword 'reporter_id' into field
# You need to specify a comparison clause
>>> Article.objects.get_list(reporter_id=1)
Traceback (most recent call last):
...
TypeError: Cannot parse keyword query 'reporter_id'
# "pk" shortcut syntax works in a related context, too.
>>> Article.objects.get_list(reporter__pk=1, order_by=['pub_date'])
@@ -109,4 +128,28 @@ John Smith
>>> a4.get_reporter()
John Smith
# Reporters can be queried
>>> Reporter.objects.get_list(id__exact=1)
[John Smith]
>>> Reporter.objects.get_list(pk=1)
[John Smith]
>>> Reporter.objects.get_list(first_name__startswith='John')
[John Smith]
# Reporters can query in opposite direction of ForeignKey definition
>>> Reporter.objects.get_list(articles__id__exact=1)
[John Smith]
>>> Reporter.objects.get_list(articles__pk=1)
[John Smith]
>>> Reporter.objects.get_list(articles__headline__startswith='This')
[John Smith, John Smith, John Smith]
>>> Reporter.objects.get_list(articles__headline__startswith='This', distinct=True)
[John Smith]
# Queries can go round in circles.
>>> Reporter.objects.get_list(articles__reporter__first_name__startswith='John')
[John Smith, John Smith, John Smith, John Smith]
>>> Reporter.objects.get_list(articles__reporter__first_name__startswith='John', distinct=True)
[John Smith]
"""

View File

@@ -66,10 +66,23 @@ DoesNotExist: Restaurant does not exist for {'place__id__exact': ...}
>>> Restaurant.objects.get_object(place__id__exact=1)
Demon Dogs the restaurant
>>> Restaurant.objects.get_object(place__name__startswith="Demon")
Demon Dogs the restaurant
>>> Restaurant.objects.get_object(pk=1)
Demon Dogs the restaurant
>>> Restaurant.objects.get_object(place__exact=1)
Demon Dogs the restaurant
>>> Restaurant.objects.get_object(place__pk=1)
Demon Dogs the restaurant
>>> Restaurant.objects.get_object(place__name__startswith="Demon")
Demon Dogs the restaurant
>>> Place.objects.get_object(id__exact=1)
Demon Dogs the place
>>> Place.objects.get_object(pk=1)
Demon Dogs the place
>>> Place.objects.get_object(restaurants__place__exact=1)
Demon Dogs the place
>>> Place.objects.get_object(restaurants__pk=1)
Demon Dogs the place
# Add a Waiter to the Restaurant.
>>> w = r.add_waiter(name='Joe')
@@ -77,6 +90,17 @@ Demon Dogs the restaurant
>>> w
Joe the waiter at Demon Dogs the restaurant
# Query the waiters
>>> Waiter.objects.get_list(restaurant__place__exact=1)
[Joe the waiter at Demon Dogs the restaurant]
>>> Waiter.objects.get_list(restaurant__pk=1)
[Joe the waiter at Demon Dogs the restaurant]
>>> Waiter.objects.get_list(id__exact=1)
[Joe the waiter at Demon Dogs the restaurant]
>>> Waiter.objects.get_list(pk=1)
[Joe the waiter at Demon Dogs the restaurant]
# Delete the restaurant; the waiter should also be removed
>>> r = Restaurant.objects.get_object(pk=1)
>>> r.delete()
"""