From aa239e3e5405933af6a29dac3cf587b59a099927 Mon Sep 17 00:00:00 2001 From: Justin Bronn Date: Tue, 5 Aug 2008 17:15:33 +0000 Subject: gis: Merged revisions 7981-8001,8003-8011,8013-8033,8035-8036,8038-8039,8041-8063,8065-8076,8078-8139,8141-8154,8156-8214 via svnmerge from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@8215 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/sql/where.py | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) (limited to 'django/db/models/sql/where.py') diff --git a/django/db/models/sql/where.py b/django/db/models/sql/where.py index 18e4bf2f7e..662d99a4a2 100644 --- a/django/db/models/sql/where.py +++ b/django/db/models/sql/where.py @@ -35,20 +35,30 @@ class WhereNode(tree.Node): storing any reference to field objects). Otherwise, the 'data' is stored unchanged and can be anything with an 'as_sql()' method. """ + # Because of circular imports, we need to import this here. + from django.db.models.base import ObjectDoesNotExist + if not isinstance(data, (list, tuple)): super(WhereNode, self).add(data, connector) return alias, col, field, lookup_type, value = data - if field: - params = field.get_db_prep_lookup(lookup_type, value) - db_type = field.db_type() - else: - # This is possible when we add a comparison to NULL sometimes (we - # don't really need to waste time looking up the associated field - # object). - params = Field().get_db_prep_lookup(lookup_type, value) - db_type = None + try: + if field: + params = field.get_db_prep_lookup(lookup_type, value) + db_type = field.db_type() + else: + # This is possible when we add a comparison to NULL sometimes + # (we don't really need to waste time looking up the associated + # field object). + params = Field().get_db_prep_lookup(lookup_type, value) + db_type = None + except ObjectDoesNotExist: + # This can happen when trying to insert a reference to a null pk. + # We break out of the normal path and indicate there's nothing to + # match. + super(WhereNode, self).add(NothingNode(), connector) + return if isinstance(value, datetime.datetime): annotation = datetime.datetime else: @@ -190,3 +200,14 @@ class EverythingNode(object): def relabel_aliases(self, change_map, node=None): return + +class NothingNode(object): + """ + A node that matches nothing. + """ + def as_sql(self, qn=None): + raise EmptyResultSet + + def relabel_aliases(self, change_map, node=None): + return + -- cgit v1.3