Django

Code

MultipleDatabaseSupport: naive-related.diff

File naive-related.diff, 2.4 kB (added by Jon Dugan <jdugan@x1024.net>, 2 years ago)

naive first pass at getting related fields to work

  • related.py

    old new  
    287287            # Add the newly created or already existing objects to the join table. 
    288288            # First find out which items are already added, to avoid adding them twice 
    289289            new_ids = set([obj._get_pk_val() for obj in objs]) 
    290             cursor = connection.cursor() 
     290            cursor = self.model._meta.connection.cursor() 
    291291            cursor.execute("SELECT %s FROM %s WHERE %s = %%s AND %s IN (%s)" % \ 
    292292                (target_col_name, self.join_table, source_col_name, 
    293293                target_col_name, ",".join(['%s'] * len(new_ids))), 
     
    302302                cursor.execute("INSERT INTO %s (%s, %s) VALUES (%%s, %%s)" % \ 
    303303                    (self.join_table, source_col_name, target_col_name), 
    304304                    [self._pk_val, obj_id]) 
    305             transaction.commit_unless_managed(
     305            transaction.commit_unless_managed(self.model._meta.connection
    306306 
    307307        def _remove_items(self, source_col_name, target_col_name, *objs): 
    308308            # source_col_name: the PK colname in join_table for the source object 
     
    314314                if not isinstance(obj, self.model): 
    315315                    raise ValueError, "objects to remove() must be %s instances" % self.model._meta.object_name 
    316316            # Remove the specified objects from the join table 
    317             cursor = connection.cursor() 
     317            cursor = self.model._meta.connection.cursor() 
    318318            for obj in objs: 
    319319                cursor.execute("DELETE FROM %s WHERE %s = %%s AND %s = %%s" % \ 
    320320                    (self.join_table, source_col_name, target_col_name), 
    321321                    [self._pk_val, obj._get_pk_val()]) 
    322             transaction.commit_unless_managed(
     322            transaction.commit_unless_managed(self.model._meta.connection
    323323 
    324324        def _clear_items(self, source_col_name): 
    325325            # source_col_name: the PK colname in join_table for the source object 
    326326            from django.db import connection 
    327             cursor = connection.cursor() 
     327            cursor = self.model._meta.connection.cursor() 
    328328            cursor.execute("DELETE FROM %s WHERE %s = %%s" % \ 
    329329                (self.join_table, source_col_name), 
    330330                [self._pk_val]) 
    331             transaction.commit_unless_managed(
     331            transaction.commit_unless_managed(self.model._meta.connection
    332332 
    333333    return ManyRelatedManager 
    334334