Example #1
0
    def ac(f):
        @wraps(f)
        def wrapper(*args, **kwargs):
            return f(*args, **kwargs)
        return wrapper

@ac
def init_enums(sender, **kwargs):
    cursor = connection.cursor()
    for name, sql in _typesql.items():
        cursor.execute("SELECT EXISTS(SELECT typname FROM pg_type WHERE typname=%s);", [name])
        result = cursor.fetchone()
        if not result[0]:
            cursor.execute(sql)
            transaction.commit_unless_managed()
register_initializer(init_enums)

def init_uuid(sender, **kwargs):
    register_uuid()

_typesql = {}

class EnumField(models.Field):
    description = "PostgreSQL enum type"
    __metaclass__ = models.SubfieldBase

    def __init__(self, *args, **kwargs):
        global _typesql
        self.enumeration = kwargs.pop('enumeration')
        self.type_name = kwargs.pop('type_name',
                                   'enum_'+hashlib.md5('|'.join(self.enumeration)).hexdigest())
Example #2
0
from django.db import models

from pgfields.utils import register_initializer
from pgfields.hstore import forms
from pgfields.hstore.query import HStoreQuerySet
from pgfields.hstore.util import acquire_reference, serialize_references, unserialize_references


def init_hstore(sender, **kwargs):
    from django.db import connection
    from psycopg2.extras import register_hstore
    register_hstore(connection.connection, unicode=True)
register_initializer(init_hstore)

class HStoreDictionary(dict):
    """A dictionary subclass which implements hstore support."""

    def __init__(self, value=None, field=None, instance=None, **params):
        value = {} if value is None else value
        super(HStoreDictionary, self).__init__(value, **params)
        self.field = field
        self.instance = instance

    def remove(self, keys):
        """Removes the specified keys from this dictionary."""

        queryset = self.instance._base_manager.get_query_set()
        queryset.filter(pk=self.instance.pk).hremove(self.field.name, keys)

class HStoreDescriptor(object):
    def __init__(self, field):