From 116e558045f2c73cecd1d139e7c311562da426c7 Mon Sep 17 00:00:00 2001 From: Mikko Hellsing Date: Sun, 5 Feb 2012 12:45:38 +0100 Subject: [PATCH] update readme and make db_index True by default --- MANIFEST.in | 2 +- README.rst | 44 +++++++++++++++++------------ django_hstore/fields.py | 4 +++ tests/django_hstore_tests/models.py | 4 +-- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/MANIFEST.in b/MANIFEST.in index 018174e..216b19b 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,3 +1,3 @@ include AUTHORS LICENSE README.rst recursive-include tests * -recursive-exclude * *.pyc +recursive-exclude * *.pyc *.swp diff --git a/README.rst b/README.rst index 19f641e..ce319be 100644 --- a/README.rst +++ b/README.rst @@ -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= HSTORE_SQL= ./runtests + python setup.py test Usage ===== @@ -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:: @@ -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): @@ -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 diff --git a/django_hstore/fields.py b/django_hstore/fields.py index 3558cc6..165f69e 100644 --- a/django_hstore/fields.py +++ b/django_hstore/fields.py @@ -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)) diff --git a/tests/django_hstore_tests/models.py b/tests/django_hstore_tests/models.py index 693d4bc..1c0d842 100644 --- a/tests/django_hstore_tests/models.py +++ b/tests/django_hstore_tests/models.py @@ -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):