Permalink
Browse files

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
  • Loading branch information...
1 parent 2bc39d8 commit f998c7bcc6d6b42b98bc3ca8e65b70b023bd3bea @adrianholovaty adrianholovaty committed Jan 8, 2006
View

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -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
@@ -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
@@ -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'
@@ -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]
+
"""
@@ -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]
@@ -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]
@@ -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 @@ 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.
@@ -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'])
@@ -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]
+
"""
@@ -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.