@@ -0,0 +1,2637 @@
+import hashlib%0Aimport uuid%0A%0Afrom psycopg2.extensions import adapt%0Afrom psycopg2.extras import register_uuid%0Afrom django.db import models%0A%0Afrom pgfields.utils import addrule, register_initializer%0A%0Afrom django.db import connection, transaction%0A@transaction.autocommit()%0Adef init_enums(sender, **kwargs):%0A    cursor = connection.cursor()%0A    for name, sql in _typesql.items():%0A        cursor.execute(%22SELECT EXISTS(SELECT typname FROM pg_type WHERE typname=%25s);%22, %5Bname%5D)%0A        result = cursor.fetchone()%0A        if not result%5B0%5D:%0A            cursor.execute(sql)%0A            transaction.commit_unless_managed()%0Aregister_initializer(init_enums)%0A%0Adef init_uuid(sender, **kwargs):%0A    register_uuid()%0A%0A_typesql = %7B%7D%0A%0Aclass EnumField(models.Field):%0A    description = %22PostgreSQL enum type%22%0A    __metaclass__ = models.SubfieldBase%0A%0A    def __init__(self, *args, **kwargs):%0A        global _typesql%0A        self.enumeration = kwargs.pop('enumeration')%0A        self.type_name = kwargs.pop('type_name',%0A                                   'enum_'+hashlib.md5('%7C'.join(self.enumeration)).hexdigest())%0A        kwargs%5B'choices'%5D = %5B(v,v) for v in self.enumeration%5D%0A        try:%0A            kwargs%5B'default'%5D = str(adapt(kwargs%5B'default'%5D).getquoted())%0A        except KeyError:%0A            pass%0A        _typesql%5Bself.type_name%5D = self.create_type()%0A        super(EnumField, self).__init__(*args, **kwargs)%0A%0A    def db_type(self, connection):%0A        return self.type_name%0A%0A    def create_type(self):%0A        return %22CREATE TYPE %25s as ENUM (%25s)%22 %25 (%0A            self.type_name, ','.join(str(adapt(v).getquoted()) for v in self.enumeration)%0A        )%0A%0Aaddrule(%5B%0A    (%0A        %5BEnumField%5D,%0A        %5B%5D,%0A        %7B%0A            'enumeration':%5B'enumeration', %7B%7D%5D,%0A            'type_name':%5B'type_name', %7B%7D%5D,%0A        %7D,%0A    ),%0A%5D, %5B'%5Epgfields%5C.basic%5C.EnumField'%5D)%0A%0Aclass UUIDField(models.Field):%0A    description = %22PostgreSQL UUID type%22%0A    __metaclass__ = models.SubfieldBase%0A%0A    def __init__(self, *args, **kwargs):%0A        self.uuid_version = kwargs.pop('uuid_version', 4)%0A        # GAH! Can't do this, django caches the default value as a static%0A        # string%0A        #kwargs%5B'default'%5D = getattr(uuid, %22uuid%25d%22 %25 self.uuid_version)()%0A        super(UUIDField, self).__init__(*args, **kwargs)%0A%0A    def get_prep_value(self, value):%0A        if value == '':%0A            value = getattr(uuid, %22uuid%25d%22 %25 self.uuid_version)()%0A            print %22returned VALUE %25s%22 %25 value%0A        return str(value)%0A%0A    def to_python(self, value):%0A        return str(value)%0A%0A    def db_type(self, connection):%0A        return 'UUID'%0A%0Aaddrule(%5B%5D, %5B'%5Epgfields%5C.basic%5C.EnumField'%5D)%0A

Add UUID field