from __future__ import absolute_import, unicode_literals
from psycopg2.extensions import adapt, SQL_IN


class CompositeAdapter(object):
    db_type = ''

    def __init__(self, obj):
        self._obj = obj

    def getquoted(self):
        """Return the appropriate SQL for the given object, typecast
        to the registered db_type.
        """
        return '{sql}::{db_type}'.format(
            db_type=self._db_type,
            sql=adapt(tuple(self._obj)).getquoted().decode('utf8'),
        ).encode('utf8')


def adapter_factory(name, db_type):
    return type(name, (CompositeAdapter,), { '_db_type': db_type })

@@ -50,16 +50,81 @@
 iterals%0A
+from django.db import connection%0Afrom psycopg2 import extensions%0A
 from psy
@@ -161,16 +161,16 @@
  SQL_IN%0A
-
 %0A%0Aclass 
@@ -408,16 +408,247 @@
     %22%22%22%0A
+        # Prepare an adapted object from the tuple form of this%0A        # composite instance.%0A        adapted = adapt(tuple(self._obj))%0A        adapted.prepare(connection.connection)%0A%0A        # Return the appropriate SQL fragment.%0A
         
@@ -737,34 +737,18 @@
 ql=adapt
-(tuple(self._obj))
+ed
 .getquot

Fix a unicode bug in the adapter.