skip to navigation
skip to content

Not Logged In

djorm-ext-hstore 0.5

Hstore module of django orm extensions package (collection of third party plugins build in one unified package).

Latest Version: 0.6

Is the library which integrates the hstore extension of PostgreSQL into Django,

Compatible with:

  • django: 1.4, 1.5, 1.6
  • python: 2.7 y 3.3+

Limitations and notes

  • 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.
  • Hstore extension is not automatically installed on use this package. You must install it manually. (For execute tests, you must install hstore extension on template1 database.
  • For run tests, hstore extension must be installed on template1 database. (Short version)

Limitation for running tests

This limitatation is affects bugs #11 and #12 (and can not be solved with other aproach than installing hstore on template1 database.

The complete explanation, a lot of thanks to Florian Demmer:

i think i got it... the oid is the problem and how and when it is determined... and maybe a little how the tests are set up... and generally it is a multi-db issue.

in test two databases are configured, but while running tests of course django creates new "test_*" databases (from "template1"). however initially django connects to one of the configured "test" or "test2" databases. i don't know which. but on this initial connect the connect-signal triggers the extension registration, which determines the hstore oid and uses it to register the extension globally.

so the extension is now registerd with the oid from "test" or "test2". if one had added the hstore extension to "template1" before creating "test" and "test2" this would be no problem, as both would have the hstore oid copied from "template1".

so in my case i did not have "template1" set up on my notebook when i created the test databases and both have different hstore oids and tests fail. i did have it set up on my pc and the test databases have the same hstore oid and test work.

so, what does it mean!?

using unique=False works around this problem by reloading the oid for every connection. the more i think about it, this is a very, very ugly workaround.

I strongly recommend install hstore on template1 for avoid strange behavior.


The library provides three principal classes:

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

NOTE: the predefined hstore manager inherits all functionality of djorm-ext-expressions module (which is part of django orm extensions package)


Initially define some sample model:

from django.db import models
from djorm_hstore.fields import DictionaryField
from djorm_hstore.models import HStoreManager

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

    def __unicode__(self):

You then treat the data field as simply a dictionary of string pairs:

instance = Something.objects.create(name='something', data={'a': '1', 'b': '2'})
assert['a'] == '1'

empty = Something.objects.create(name='empty')
assert == {}['a'] = '1'
assert Something.objects.get(name='something').data['a'] == '1'

You can issue indexed queries against hstore fields:

from djorm_hstore.expressions import HstoreExpression as HE

# equivalence
Something.objects.filter(data={'a': '1', 'b': '2'})

# subset by key/value mapping

# subset by list of keys
Something.objects.where(HE("data").contains(['a', 'b']))

# subset by single key

You can also take advantage of some db-side functionality by using the manager:

# identify the keys present in an hstore field
>>> Something.objects.filter(id=1).hkeys(attr='data')
['a', 'b']

# peek at a a named value within an hstore field
>>> Something.objects.filter(id=1).hpeek(attr='data', key='a')

# remove a key/value pair from an hstore field
>>> Something.objects.filter(name='something').hremove('data', 'b')

In addition to filters and specific methods to retrieve keys or hstore field values, we can also use annotations, and then we can filter for them.

from djorm_hstore.functions import HstoreSlice, HstorePeek, HstoreKeys

queryset = SomeModel.objects.annotate_functions(
    sliced = HstoreSlice("hstorefield", ['v']),
    peeked = HstorePeek("hstorefield", "v"),
    keys = HstoreKeys("hstorefield"),

Psycopg2 hstore registration

If for some reason you have to use djorm_hstore along databases that don't have hstore extension installed, you can skip hstore registration by setting HAS_HSTORE to False in your database config:

    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'name',
        'USER': 'user',
        'PASSWORD': 'pass',
        'HOST': 'localhost',
        'PORT': '',
    'other': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'other',
        'USER': 'user',
        'PASSWORD': 'pass',
        'HOST': 'localhost',
        'PORT': '',
        'HAS_HSTORE': False,

If you do that, then don't try to create DictionaryField in this database. Be sure to check out allow_syncdb documentation.

File Type Py Version Uploaded on Size
djorm-ext-hstore-0.5.tar.gz (md5) Source 2013-07-27 10KB
  • Downloads (All Versions):
  • 200 downloads in the last day
  • 337 downloads in the last week
  • 1142 downloads in the last month