Fixed #7823 -- Fixed an edge case in RelatedField.get_db_prep_lookup() so that

it works correctly with custom field subclasses. Patch from Ivan Sagalaev.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@8690 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2008-08-29 02:40:50 +00:00
parent d22ee35ee9
commit a1d2f6ef0c
1 changed files with 9 additions and 3 deletions

View File

@ -125,12 +125,18 @@ class RelatedField(object):
# that object. In certain conditions (especially one-to-one relations), # that object. In certain conditions (especially one-to-one relations),
# the primary key may itself be an object - so we need to keep drilling # the primary key may itself be an object - so we need to keep drilling
# down until we hit a value that can be used for a comparison. # down until we hit a value that can be used for a comparison.
v = value v, field = value, None
try: try:
while True: while True:
v = getattr(v, v._meta.pk.name) v, field = getattr(v, v._meta.pk.name), v._meta.pk
except AttributeError: except AttributeError:
pass pass
if field:
if lookup_type in ('range', 'in'):
v = [v]
v = field.get_db_prep_lookup(lookup_type, v)
if isinstance(v, list):
v = v[0]
return v return v
if hasattr(value, 'as_sql'): if hasattr(value, 'as_sql'):
@ -138,7 +144,7 @@ class RelatedField(object):
return QueryWrapper(('(%s)' % sql), params) return QueryWrapper(('(%s)' % sql), params)
if lookup_type == 'exact': if lookup_type == 'exact':
return [pk_trace(value)] return [pk_trace(value)]
if lookup_type == 'in': if lookup_type in ('range', 'in'):
return [pk_trace(v) for v in value] return [pk_trace(v) for v in value]
elif lookup_type == 'isnull': elif lookup_type == 'isnull':
return [] return []