Author: russellm
Date: 2010-03-27 10:16:27 -0500 (Sat, 27 Mar 2010)
New Revision: 12865

Modified:
   django/trunk/django/db/models/fields/related.py
   django/trunk/django/db/models/query_utils.py
   django/trunk/tests/regressiontests/queries/models.py
Log:
Fixed #13227 -- Modified ForeignKeys to fully honor the db_prep/prep separation 
introduced by multidb. This was required to ensure that model instances aren't 
deepcopied as a result of being involved in a filter clause. Thanks to claudep 
for the report, and Alex Gaynor for the help on the patch.

Modified: django/trunk/django/db/models/fields/related.py
===================================================================
--- django/trunk/django/db/models/fields/related.py     2010-03-27 12:35:59 UTC 
(rev 12864)
+++ django/trunk/django/db/models/fields/related.py     2010-03-27 15:16:27 UTC 
(rev 12865)
@@ -121,32 +121,24 @@
         if not cls._meta.abstract:
             self.contribute_to_related_class(other, self.related)
 
+    def get_prep_lookup(self, lookup_type, value):
+        if hasattr(value, 'prepare'):
+            return value.prepare()
+        if hasattr(value, '_prepare'):
+            return value._prepare()
+        # FIXME: lt and gt are explicitly allowed to make
+        # get_(next/prev)_by_date work; other lookups are not allowed since 
that
+        # gets messy pretty quick. This is a good candidate for some 
refactoring
+        # in the future.
+        if lookup_type in ['exact', 'gt', 'lt', 'gte', 'lte']:
+            return self._pk_trace(value, 'get_prep_lookup', lookup_type)
+        if lookup_type in ('range', 'in'):
+            return [self._pk_trace(v, 'get_prep_lookup', lookup_type) for v in 
value]
+        elif lookup_type == 'isnull':
+            return []
+        raise TypeError("Related Field has invalid lookup: %s" % lookup_type)
+
     def get_db_prep_lookup(self, lookup_type, value, connection, 
prepared=False):
-        # If we are doing a lookup on a Related Field, we must be
-        # comparing object instances. The value should be the PK of value,
-        # not value itself.
-        def pk_trace(value):
-            # Value may be a primary key, or an object held in a relation.
-            # If it is an object, then we need to get the primary key value for
-            # that object. In certain conditions (especially one-to-one 
relations),
-            # 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.
-            v, field = value, None
-            try:
-                while True:
-                    v, field = getattr(v, v._meta.pk.name), v._meta.pk
-            except AttributeError:
-                pass
-
-            if field:
-                if lookup_type in ('range', 'in'):
-                    v = [v]
-                v = field.get_db_prep_lookup(lookup_type, v,
-                        connection=connection, prepared=prepared)
-                if isinstance(v, list):
-                    v = v[0]
-            return v
-
         if not prepared:
             value = self.get_prep_lookup(lookup_type, value)
         if hasattr(value, 'get_compiler'):
@@ -162,18 +154,50 @@
                 sql, params = value._as_sql(connection=connection)
             return QueryWrapper(('(%s)' % sql), params)
 
-        # FIXME: lt and gt are explicitally allowed to make
+        # FIXME: lt and gt are explicitly allowed to make
         # get_(next/prev)_by_date work; other lookups are not allowed since 
that
         # gets messy pretty quick. This is a good candidate for some 
refactoring
         # in the future.
         if lookup_type in ['exact', 'gt', 'lt', 'gte', 'lte']:
-            return [pk_trace(value)]
+            return [self._pk_trace(value, 'get_db_prep_lookup', lookup_type,
+                            connection=connection, prepared=prepared)]
         if lookup_type in ('range', 'in'):
-            return [pk_trace(v) for v in value]
+            return [self._pk_trace(v, 'get_db_prep_lookup', lookup_type,
+                            connection=connection, prepared=prepared)
+                    for v in value]
         elif lookup_type == 'isnull':
             return []
         raise TypeError("Related Field has invalid lookup: %s" % lookup_type)
 
+    def _pk_trace(self, value, prep_func, lookup_type, **kwargs):
+        # Value may be a primary key, or an object held in a relation.
+        # If it is an object, then we need to get the primary key value for
+        # that object. In certain conditions (especially one-to-one relations),
+        # 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.
+        v = value
+        try:
+            while True:
+                v = getattr(v, v._meta.pk.name)
+        except AttributeError:
+            pass
+        except exceptions.ObjectDoesNotExist:
+            v = None
+
+        field = self
+        while field.rel:
+            if hasattr(field.rel, 'field_name'):
+                field = field.rel.to._meta.get_field(field.rel.field_name)
+            else:
+                field = field.rel.to._meta.pk
+
+        if lookup_type in ('range', 'in'):
+            v = [v]
+        v = getattr(field, prep_func)(lookup_type, v, **kwargs)
+        if isinstance(v, list):
+            v = v[0]
+        return v
+
     def _get_related_query_name(self, opts):
         # This method defines the name that can be used to identify this
         # related object in a table-spanning query. It uses the lower-cased

Modified: django/trunk/django/db/models/query_utils.py
===================================================================
--- django/trunk/django/db/models/query_utils.py        2010-03-27 12:35:59 UTC 
(rev 12864)
+++ django/trunk/django/db/models/query_utils.py        2010-03-27 15:16:27 UTC 
(rev 12865)
@@ -155,7 +155,8 @@
     def _combine(self, other, conn):
         if not isinstance(other, Q):
             raise TypeError(other)
-        obj = deepcopy(self)
+        obj = type(self)()
+        obj.add(self, conn)
         obj.add(other, conn)
         return obj
 
@@ -166,7 +167,8 @@
         return self._combine(other, self.AND)
 
     def __invert__(self):
-        obj = deepcopy(self)
+        obj = type(self)()
+        obj.add(self, self.AND)
         obj.negate()
         return obj
 

Modified: django/trunk/tests/regressiontests/queries/models.py
===================================================================
--- django/trunk/tests/regressiontests/queries/models.py        2010-03-27 
12:35:59 UTC (rev 12864)
+++ django/trunk/tests/regressiontests/queries/models.py        2010-03-27 
15:16:27 UTC (rev 12865)
@@ -5,6 +5,7 @@
 import datetime
 import pickle
 import sys
+import threading
 
 from django.conf import settings
 from django.db import models, DEFAULT_DB_ALIAS
@@ -45,6 +46,13 @@
     def __unicode__(self):
         return self.note
 
+    def __init__(self, *args, **kwargs):
+        super(Note, self).__init__(*args, **kwargs)
+        # Regression for #13227 -- having an attribute that
+        # is unpickleable doesn't stop you from cloning queries
+        # that use objects of that type as an argument.
+        self.lock = threading.Lock()
+
 class Annotation(models.Model):
     name = models.CharField(max_length=10)
     tag = models.ForeignKey(Tag)

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to