From 14d6ee2dc956d06fc1014e1b3c1f1410d553157d Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Mon, 28 Apr 2008 21:15:05 +0000 Subject: [PATCH] Changed Query.get_columns() to quote the 'AS' column names in an extra_select situation, to match pre-queryset-refactor behavior. Added unit tests that verify this and provide an example git-svn-id: http://code.djangoproject.com/svn/django/trunk@7502 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/sql/query.py | 3 ++- tests/modeltests/basic/models.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 7e9fb00418..a6957bab7b 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -394,7 +394,8 @@ class Query(object): some cases to avoid ambiguitity with nested queries. """ qn = self.quote_name_unless_alias - result = ['(%s) AS %s' % (col, alias) for alias, col in self.extra_select.iteritems()] + qn2 = self.connection.ops.quote_name + result = ['(%s) AS %s' % (col, qn2(alias)) for alias, col in self.extra_select.iteritems()] aliases = set(self.extra_select.keys()) if with_aliases: col_aliases = aliases.copy() diff --git a/tests/modeltests/basic/models.py b/tests/modeltests/basic/models.py index 51de8a50f8..d7c27cb15b 100644 --- a/tests/modeltests/basic/models.py +++ b/tests/modeltests/basic/models.py @@ -398,4 +398,16 @@ u'\u6797\u539f \u3081\u3050\u307f' >>> s = set([a10, a11, a12]) >>> Article.objects.get(headline='Article 11') in s True + +# The 'select' argument to extra() supports names with dashes in them, as long +# as you use values(). +>>> Article.objects.filter(pub_date__year=2008).extra(select={'dashed-value': '1'}).values('headline', 'dashed-value') +[{'headline': u'Article 11', 'dashed-value': 1}, {'headline': u'Article 12', 'dashed-value': 1}] + +# If you use 'select' with extra() and names containing dashes on a query +# that's *not* a values() query, those extra 'select' values will silently be +# ignored. +>>> articles = Article.objects.filter(pub_date__year=2008).extra(select={'dashed-value': '1', 'undashedvalue': '2'}) +>>> articles[0].undashedvalue +2 """