Skip to content

Commit

Permalink
update readme and make db_index True by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikko Hellsing committed Feb 5, 2012
1 parent 8325ae8 commit 116e558
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
include AUTHORS LICENSE README.rst
recursive-include tests *
recursive-exclude * *.pyc
recursive-exclude * *.pyc *.swp
44 changes: 26 additions & 18 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@
django-hstore
=============

Django-hstore is a niche library which integrates the `hstore`_ extension of PostgreSQL into Django,
assuming one is using Django 1.2+, PostgreSQL 9.0+, and Psycopg 2.3+.
Django-hstore is a niche library which integrates the `hstore`_ extension of
PostgreSQL into Django, assuming one is using Django 1.2+, PostgreSQL 9.0+, and
Psycopg 2.3+.

Limitations
===========

- Due to how Django implements its ORM, you will need to use the custom ``postgresql_psycopg2`` backend
defined in this package, which naturally will prevent you from dropping in other django extensions
which require a custom backend (unless you fork and combine).
- PostgreSQL's implementation of hstore has no concept of type; it stores a mapping of string keys to
string values. This library makes no attempt to coerce keys or values to strings.
- Due to how Django implements its ORM, you will need to use the custom
``postgresql_psycopg2`` backend defined in this package, which naturally will
prevent you from dropping in other django extensions which require a custom
backend (unless you fork and combine).
- PostgreSQL's implementation of hstore has no concept of type; it stores a
mapping of string keys to string values. This library makes no attempt to
coerce keys or values to strings.

Running the tests
=================

Assuming one has the dependencies installed as well as nose, and a PostgreSQL 9.0+ server up and running::
Assuming one has the dependencies installed, and a PostgreSQL 9.0+ server up and
running::

DB_USER=<username> HSTORE_SQL=<path-to-contrib/hstore.sql> ./runtests
python setup.py test

Usage
=====
Expand All @@ -33,19 +37,23 @@ First, update your settings module to specify the custom database backend::
}
}
**Note to South users:** If you keep getting errors like `There is no South database module 'south.db.None' for your database.`, add the following to `settings.py`::
**Note to South users:** If you keep getting errors like `There is no South
database module 'south.db.None' for your database.`, add the following to
`settings.py`::

SOUTH_DATABASE_ADAPTERS = {'default': 'south.db.postgresql_psycopg2'}

The library provides three principal classes:

``django_hstore.hstore.DictionaryField``
An ORM field which stores a mapping of string key/value pairs in an hstore column.
An ORM field which stores a mapping of string key/value pairs in an hstore
column.
``django_hstore.hstore.ReferencesField``
An ORM field which builds on DictionaryField to store a mapping of string keys to
django object references, much like ForeignKey.
An ORM field which builds on DictionaryField to store a mapping of string
keys to django object references, much like ForeignKey.
``django_hstore.hstore.HStoreManager``
An ORM manager which provides much of the query functionality of the library.
An ORM manager which provides much of the query functionality of the
library.

Model definition is straightforward::

Expand All @@ -54,7 +62,7 @@ Model definition is straightforward::

class Something(models.Model):
name = models.CharField(max_length=32)
data = hstore.DictionaryField(db_index=True)
data = hstore.DictionaryField()
objects = hstore.HStoreManager()

def __unicode__(self):
Expand Down Expand Up @@ -103,8 +111,8 @@ You can also take advantage of some db-side functionality by using the manager::
# remove a key/value pair from an hstore field
>>> Something.objects.filter(name='something').hremove('data', 'b')

The hstore methods on manager pass all keyword arguments aside from ``attr`` and ``key``
to ``.filter()``.
The hstore methods on manager pass all keyword arguments aside from ``attr`` and
``key`` to ``.filter()``.

.. _hstore: http://www.postgresql.org/docs/9.0/interactive/hstore.html
.. _hstore: http://www.postgresql.org/docs/9.1/interactive/hstore.html

4 changes: 4 additions & 0 deletions django_hstore/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def __set__(self, obj, value):


class HStoreField(models.Field):
def __init__(self, *args, **kwargs):
kwargs.setdefault('db_index', True)
super(HStoreField, self).__init__(*args, **kwargs)

def contribute_to_class(self, cls, name):
super(HStoreField, self).contribute_to_class(cls, name)
setattr(cls, self.name, HStoreDescriptor(self))
Expand Down
4 changes: 2 additions & 2 deletions tests/django_hstore_tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ class Meta:

class DataBag(HStoreModel):
name = models.CharField(max_length=32)
data = hstore.DictionaryField(db_index=True)
data = hstore.DictionaryField()


class RefsBag(HStoreModel):
name = models.CharField(max_length=32)
refs = hstore.ReferencesField(db_index=True)
refs = hstore.ReferencesField()


class DefaultsModel(models.Model):
Expand Down

0 comments on commit 116e558

Please sign in to comment.