Skip to content

Commit

Permalink
magic-removal: Fixed #1186 -- Fixed problem resolving primary key in …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
adrianholovaty committed Jan 8, 2006
1 parent 2bc39d8 commit f998c7b
Show file tree
Hide file tree
Showing 7 changed files with 335 additions and 174 deletions.
364 changes: 195 additions & 169 deletions django/db/models/query.py

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions django/utils/datastructures.py
Expand Up @@ -40,6 +40,41 @@ def has_key(self, key):
return True
return False

class SortedDict(dict):
"A dictionary that keeps its keys in the order in which they're inserted."
def __init__(self, data={}):
dict.__init__(self, data)
self.keyOrder = data.keys()

def __setitem__(self, key, value):
dict.__setitem__(self, key, value)
if key not in self.keyOrder:
self.keyOrder.append(key)

def __delitem__(self, key, value):
dict.__delitem__(self, key, value)
self.keyOrder.remove(key)

def __iter__(self):
for k in self.keyOrder:
yield k

def items(self):
for k in self.keyOrder:
yield k, dict.__getitem__(self, k)

def keys(self):
for k in self.keyOrder:
yield k

def values(self):
for k in self.keyOrder:
yield dict.__getitem__(self, k)

def update(self, dict):
for k, v in dict.items():
self.__setitem__(k, v)

class MultiValueDictKeyError(KeyError):
pass

Expand Down
2 changes: 1 addition & 1 deletion tests/modeltests/custom_columns/models.py
Expand Up @@ -35,7 +35,7 @@ def __repr__(self):
>>> 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
Expand Down
23 changes: 23 additions & 0 deletions tests/modeltests/custom_pk/models.py
Expand Up @@ -47,6 +47,10 @@ def __repr__(self):
...
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'
Expand All @@ -66,4 +70,23 @@ def __repr__(self):
[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]
"""
10 changes: 10 additions & 0 deletions tests/modeltests/many_to_many/models.py
Expand Up @@ -68,6 +68,8 @@ def __repr__(self):
[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]
Expand All @@ -78,9 +80,17 @@ def __repr__(self):
[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]
Expand Down
47 changes: 45 additions & 2 deletions tests/modeltests/many_to_one/models.py
Expand Up @@ -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')
Expand Down Expand Up @@ -60,6 +61,16 @@ def __repr__(self):
>>> 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.
Expand All @@ -83,12 +94,20 @@ def __repr__(self):
# 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'])
Expand All @@ -109,4 +128,28 @@ def __repr__(self):
>>> 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]
"""
28 changes: 26 additions & 2 deletions tests/modeltests/one_to_one/models.py
Expand Up @@ -66,17 +66,41 @@ def __repr__(self):
>>> 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')
>>> w.save()
>>> 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()
"""

0 comments on commit f998c7b

Please sign in to comment.