﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
18416	Query fails when SimpleLazyObject evaluates to None	Bouke Haarsma	nobody	"Relevant excerpt from model definition:
{{{
class Event(models.Model):
    season = models.ForeignKey(Season)
}}}

Expected result:
{{{
>>> Event.objects.filter(season=None)
[]
}}}

Actual result:
{{{
>>> from django.utils.functional import SimpleLazyObject
>>> lazy = SimpleLazyObject(lambda: None)
>>> lazy
<django.utils.functional.SimpleLazyObject object at 0x10203cd90>
>>> print lazy
None
>>> Event.objects.filter(season=lazy)
Traceback (most recent call last):
  File ""<console>"", line 1, in <module>
  File ""django/db/models/manager.py"", line 143, in filter
    return self.get_query_set().filter(*args, **kwargs)
  File ""django/db/models/query.py"", line 621, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File ""django/db/models/query.py"", line 639, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File ""django/db/models/sql/query.py"", line 1250, in add_q
    can_reuse=used_aliases, force_having=force_having)
  File ""django/db/models/sql/query.py"", line 1185, in add_filter
    connector)
  File ""django/db/models/sql/where.py"", line 69, in add
    value = obj.prepare(lookup_type, value)
  File ""django/db/models/sql/where.py"", line 320, in prepare
    return self.field.get_prep_lookup(lookup_type, value)
  File ""django/db/models/fields/related.py"", line 137, in get_prep_lookup
    return self._pk_trace(value, 'get_prep_lookup', lookup_type)
  File ""django/db/models/fields/related.py"", line 210, in _pk_trace
    v = getattr(field, prep_func)(lookup_type, v, **kwargs)
  File ""django/db/models/fields/__init__.py"", line 310, in get_prep_lookup
    return self.get_prep_value(value)
  File ""django/db/models/fields/__init__.py"", line 537, in get_prep_value
    return int(value)
TypeError: int() argument must be a string or a number, not 'SimpleLazyObject'
}}}

The `SimpleLazyObject` initialization is somewhat more complicated then depicted above, but it can evaluate to `None`. However, if it evaluates to `None`, it cannot be used as-is in the query and results in the exception as listed above."	Bug	closed	Database layer (models, ORM)	1.4	Normal	wontfix		dougvanhorn	Unreviewed	0	0	0	0	0	0
