From 7a30b4e03ae8848792bd114741bc2fb175d280a4 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Fri, 1 Jun 2007 06:19:01 +0000 Subject: [PATCH] unicode: Merged from trunk up to [5398]. git-svn-id: http://code.djangoproject.com/svn/django/branches/unicode@5399 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/query.py | 20 +++++++++++++++++--- django/oldforms/__init__.py | 2 +- django/template/defaulttags.py | 4 ++-- django/test/{doctest.py => _doctest.py} | 5 +++++ django/test/simple.py | 3 ++- django/test/testcases.py | 3 ++- docs/testing.txt | 4 ++-- tests/modeltests/lookup/models.py | 21 +++++++++++++++++++++ 8 files changed, 52 insertions(+), 10 deletions(-) rename django/test/{doctest.py => _doctest.py} (99%) diff --git a/django/db/models/query.py b/django/db/models/query.py index 666924eb79..930552ad47 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -555,9 +555,8 @@ class QuerySet(object): class ValuesQuerySet(QuerySet): def __init__(self, *args, **kwargs): super(ValuesQuerySet, self).__init__(*args, **kwargs) - # select_related and select aren't supported in values(). + # select_related isn't supported in values(). self._select_related = False - self._select = {} def iterator(self): try: @@ -567,13 +566,28 @@ class ValuesQuerySet(QuerySet): # self._fields is a list of field names to fetch. if self._fields: - columns = [self.model._meta.get_field(f, many_to_many=False).column for f in self._fields] + #columns = [self.model._meta.get_field(f, many_to_many=False).column for f in self._fields] + if not self._select: + columns = [self.model._meta.get_field(f, many_to_many=False).column for f in self._fields] + else: + columns = [] + for f in self._fields: + if f in [field.name for field in self.model._meta.fields]: + columns.append( self.model._meta.get_field(f, many_to_many=False).column ) + elif not self._select.has_key( f ): + raise FieldDoesNotExist, '%s has no field named %r' % ( self.model._meta.object_name, f ) + field_names = self._fields else: # Default to all fields. columns = [f.column for f in self.model._meta.fields] field_names = [f.attname for f in self.model._meta.fields] select = ['%s.%s' % (backend.quote_name(self.model._meta.db_table), backend.quote_name(c)) for c in columns] + + # Add any additional SELECTs. + if self._select: + select.extend(['(%s) AS %s' % (quote_only_if_word(s[1]), backend.quote_name(s[0])) for s in self._select.items()]) + cursor = connection.cursor() cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params) while 1: diff --git a/django/oldforms/__init__.py b/django/oldforms/__init__.py index bc787f860c..f5407273c3 100644 --- a/django/oldforms/__init__.py +++ b/django/oldforms/__init__.py @@ -792,7 +792,7 @@ class DecimalField(TextField): try: import decimal except ImportError: - from django.utils import decimal + from django.utils import _decimal as decimal try: return decimal.Decimal(data) except decimal.InvalidOperation, e: diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py index f2453dff4a..5d2d8a57e0 100644 --- a/django/template/defaulttags.py +++ b/django/template/defaulttags.py @@ -238,7 +238,7 @@ class RegroupNode(Node): return '' output = [] # list of dictionaries in the format {'grouper': 'key', 'list': [list of contents]} for obj in obj_list: - grouper = self.expression.resolve(Context({'var': obj}), True) + grouper = self.expression.resolve(obj, True) # TODO: Is this a sensible way to determine equality? if output and repr(output[-1]['grouper']) == repr(grouper): output[-1]['list'].append(obj) @@ -848,7 +848,7 @@ def regroup(parser, token): if lastbits_reversed[1][::-1] != 'as': raise TemplateSyntaxError, "next-to-last argument to 'regroup' tag must be 'as'" - expression = parser.compile_filter('var.%s' % lastbits_reversed[2][::-1]) + expression = parser.compile_filter(lastbits_reversed[2][::-1]) var_name = lastbits_reversed[0][::-1] return RegroupNode(target, expression, var_name) diff --git a/django/test/doctest.py b/django/test/_doctest.py similarity index 99% rename from django/test/doctest.py rename to django/test/_doctest.py index 3b364f0a75..8777a2cbba 100644 --- a/django/test/doctest.py +++ b/django/test/_doctest.py @@ -1,3 +1,8 @@ +# This is a slightly modified version of the doctest.py that shipped with Python 2.4 +# It incorporates changes that have been submitted the the Python ticket tracker +# as ticket #1521051. These changes allow for a DoctestRunner and Doctest base +# class to be specified when constructing a DoctestSuite. + # Module doctest. # Released to the public domain 16-Jan-2001, by Tim Peters (tim@python.org). # Major enhancements and refactoring by: diff --git a/django/test/simple.py b/django/test/simple.py index cfaa09a0a4..5f7f86f220 100644 --- a/django/test/simple.py +++ b/django/test/simple.py @@ -1,5 +1,6 @@ -import unittest, doctest +import unittest from django.conf import settings +from django.test import _doctest as doctest from django.test.utils import setup_test_environment, teardown_test_environment from django.test.utils import create_test_db, destroy_test_db from django.test.testcases import OutputChecker, DocTestRunner diff --git a/django/test/testcases.py b/django/test/testcases.py index 439dc8ba9e..bc4cde226c 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -1,8 +1,9 @@ -import re, doctest, unittest +import re, unittest from urlparse import urlparse from django.db import transaction from django.core import management, mail from django.db.models import get_apps +from django.test import _doctest as doctest from django.test.client import Client normalize_long_ints = lambda s: re.sub(r'(?>> for d in Article.objects.extra( select={'id_plus_one' : 'id + 1'} ).values('id', 'id_plus_one'): +... i = d.items() +... i.sort() +... i +[('id', 5), ('id_plus_one', 6)] +[('id', 6), ('id_plus_one', 7)] +[('id', 4), ('id_plus_one', 5)] +[('id', 2), ('id_plus_one', 3)] +[('id', 3), ('id_plus_one', 4)] +[('id', 7), ('id_plus_one', 8)] +[('id', 1), ('id_plus_one', 2)] + +# however, an exception FieldDoesNotExist will still be thrown +# if you try to access non-existent field (field that is neither on the model nor extra) +>>> Article.objects.extra( select={'id_plus_one' : 'id + 1'} ).values('id', 'id_plus_two') +Traceback (most recent call last): + ... +FieldDoesNotExist: Article has no field named 'id_plus_two' + # if you don't specify which fields, all are returned >>> list(Article.objects.filter(id=5).values()) == [{'id': 5, 'headline': 'Article 5', 'pub_date': datetime(2005, 8, 1, 9, 0)}] True