diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index b7438e02b25..273416559cd 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -198,9 +198,9 @@ class Field(object): # be invoked before the final SQL is evaluated if hasattr(value, 'relabel_aliases'): return value - try: + if hasattr(value, 'as_sql'): sql, params = value.as_sql() - except AttributeError: + else: sql, params = value._as_sql() return QueryWrapper(('(%s)' % sql), params) diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 8f874bc7e0d..9aa2421f034 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -145,9 +145,9 @@ class RelatedField(object): # be invoked before the final SQL is evaluated if hasattr(value, 'relabel_aliases'): return value - try: + if hasattr(value, 'as_sql'): sql, params = value.as_sql() - except AttributeError: + else: sql, params = value._as_sql() return QueryWrapper(('(%s)' % sql), params)