Skip to content

Commit

Permalink
further iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanm committed Jan 20, 2011
1 parent a8047a0 commit 851ae91
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 35 deletions.
4 changes: 0 additions & 4 deletions django_hstore/__init__.py
@@ -1,4 +0,0 @@

from django_hstore.fields import DictionaryField, ReferencesField
from django_hstore.models import HStoreManager

35 changes: 23 additions & 12 deletions django_hstore/fields.py → django_hstore/hstore.py
@@ -1,12 +1,15 @@

from django.db.models import Field, SubfieldBase
from django.db import models

from django_hstore import forms
from django_hstore import util
from django_hstore.query import HStoreQuerySet
from django_hstore.util import serialize_references, unserialize_references

class DictionaryField(Field):
class DictionaryField(models.Field):
"""Stores a python dictionary in a postgresql hstore field."""

__metaclass__ = models.SubfieldBase

def db_type(self):
return 'hstore'

Expand All @@ -17,10 +20,13 @@ def formfield(self, **params):
def get_prep_lookup(self, lookup, value):
return value

class ReferencesField(Field):
def to_python(self, value):
return value or {}

class ReferencesField(models.Field):
"""Stores a python dictionary of references to model instances in an hstore field."""

__metaclass__ = SubfieldBase
__metaclass__ = models.SubfieldBase

def db_type(self):
return 'hstore'
Expand All @@ -30,20 +36,25 @@ def formfield(self, **params):
return super(ReferencesField, self).formfield(**params)

def get_prep_lookup(self, lookup, value):
return self.get_prep_value(value)
return (serialize_references(value) if isinstance(value, dict) else value)

def get_prep_value(self, value):
if isinstance(value, dict):
return util.serialize_references(value)
else:
return value
return (serialize_references(value) if value else {})

def to_python(self, value):
return util.unserialize_references(value)
return (unserialize_references(value) if value else {})

class Manager(models.Manager):
"""Object manager which enables hstore features."""

use_for_related_fields = True

def get_query_set(self):
return HStoreQuerySet(self.model, using=self._db)

try:
from south.modelsinspector import add_introspection_rules
add_introspection_rules(rules=[], patterns=['django_hstore\.fields'])
add_introspection_rules(rules=[], patterns=['django_hstore\.hstore'])
except ImportError:
pass

14 changes: 2 additions & 12 deletions django_hstore/models.py → django_hstore/query.py
Expand Up @@ -5,8 +5,6 @@
from django.db.models.sql.query import Query
from django.db.models.sql.where import EmptyShortCircuit, WhereNode

from django_hstore.util import dict_to_hstore

class HStoreWhereNode(WhereNode):
def make_atom(self, child, qn, connection):
lvalue, lookup_type, value_annot, param = child
Expand All @@ -18,12 +16,12 @@ def make_atom(self, child, qn, connection):
field = self.sql_for_columns(lvalue, qn, connection)
if lookup_type == 'exact':
if isinstance(param, dict):
return ('%s = %%s' % field, [dict_to_hstore(param)])
return ('%s = %%s' % field, [param])
else:
raise ValueError('invalid value')
elif lookup_type == 'contains':
if isinstance(param, dict):
return ('%s @> %%s' % field, [dict_to_hstore(param)])
return ('%s @> %%s' % field, [param])
elif isinstance(param, (list, tuple)):
return ('%s ?& ARRAY[%s]' % (field, ','.join(['%s'] * len(param))), param)
elif isinstance(param, basestring):
Expand All @@ -44,11 +42,3 @@ def __init__(self, model=None, query=None, using=None):
query = query or HStoreQuery(model)
super(HStoreQuerySet, self).__init__(model=model, query=query, using=using)

class HStoreManager(Manager):
"""Object manager which enables hstore features."""

use_for_related_fields = True

def get_query_set(self):
return HStoreQuerySet(self.model, using=self._db)

7 changes: 0 additions & 7 deletions django_hstore/util.py
Expand Up @@ -12,13 +12,6 @@ def acquire_reference(reference):
except Exception:
raise ValueError()

def dict_to_hstore(value):
pairs = []
for k, v in value.iteritems():
pairs.append('"%s"=>"%s"' % (k, v))
else:
return ','.join(pairs)

def identify_instance(instance):
implementation = type(instance)
return '%s.%s:%s' % (implementation.__module__, implementation.__name__, instance.pk)
Expand Down

0 comments on commit 851ae91

Please sign in to comment.