Skip to content

Commit

Permalink
To avoid an unfortunately common user-error, rename QuerySet.as_sql().
Browse files Browse the repository at this point in the history
This was never a public API method, so this is backwards compatible, unless
you're poking at the internals. Refs #10352.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9928 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
malcolmt committed Mar 1, 2009
1 parent c6a404d commit 3242df1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
7 changes: 5 additions & 2 deletions django/db/models/fields/__init__.py
Expand Up @@ -193,12 +193,15 @@ def get_db_prep_save(self, value):


def get_db_prep_lookup(self, lookup_type, value): def get_db_prep_lookup(self, lookup_type, value):
"Returns field's value prepared for database lookup." "Returns field's value prepared for database lookup."
if hasattr(value, 'as_sql'): if hasattr(value, 'as_sql') or hasattr(value, '_as_sql'):
# If the value has a relabel_aliases method, it will need to # If the value has a relabel_aliases method, it will need to
# be invoked before the final SQL is evaluated # be invoked before the final SQL is evaluated
if hasattr(value, 'relabel_aliases'): if hasattr(value, 'relabel_aliases'):
return value return value
sql, params = value.as_sql() try:
sql, params = value.as_sql()
except AttributeError:
sql, params = value._as_sql()
return QueryWrapper(('(%s)' % sql), params) return QueryWrapper(('(%s)' % sql), params)


if lookup_type in ('regex', 'iregex', 'month', 'day', 'week_day', 'search'): if lookup_type in ('regex', 'iregex', 'month', 'day', 'week_day', 'search'):
Expand Down
7 changes: 5 additions & 2 deletions django/db/models/fields/related.py
Expand Up @@ -140,12 +140,15 @@ def pk_trace(value):
v = v[0] v = v[0]
return v return v


if hasattr(value, 'as_sql'): if hasattr(value, 'as_sql') or hasattr(value, '_as_sql'):
# If the value has a relabel_aliases method, it will need to # If the value has a relabel_aliases method, it will need to
# be invoked before the final SQL is evaluated # be invoked before the final SQL is evaluated
if hasattr(value, 'relabel_aliases'): if hasattr(value, 'relabel_aliases'):
return value return value
sql, params = value.as_sql() try:
sql, params = value.as_sql()
except AttributeError:
sql, params = value._as_sql()
return QueryWrapper(('(%s)' % sql), params) return QueryWrapper(('(%s)' % sql), params)


# FIXME: lt and gt are explicitally allowed to make # FIXME: lt and gt are explicitally allowed to make
Expand Down
7 changes: 2 additions & 5 deletions django/db/models/query.py
Expand Up @@ -703,12 +703,9 @@ def _setup_aggregate_query(self, aggregates):
self.query.add_fields(field_names, False) self.query.add_fields(field_names, False)
self.query.set_group_by() self.query.set_group_by()


def as_sql(self): def _as_sql(self):
""" """
Returns the internal query's SQL and parameters (as a tuple). Returns the internal query's SQL and parameters (as a tuple).
This is a private (internal) method. The name is chosen to provide
uniformity with other interfaces (in particular, the Query class).
""" """
obj = self.values("pk") obj = self.values("pk")
return obj.query.as_nested_sql() return obj.query.as_nested_sql()
Expand Down Expand Up @@ -812,7 +809,7 @@ def _setup_aggregate_query(self, aggregates):


super(ValuesQuerySet, self)._setup_aggregate_query(aggregates) super(ValuesQuerySet, self)._setup_aggregate_query(aggregates)


def as_sql(self): def _as_sql(self):
""" """
For ValueQuerySet (and subclasses like ValuesListQuerySet), they can For ValueQuerySet (and subclasses like ValuesListQuerySet), they can
only be used as nested queries if they're already set up to select only only be used as nested queries if they're already set up to select only
Expand Down

0 comments on commit 3242df1

Please sign in to comment.