Package: djorm-ext-hstore
This library is nowDEPRECATEDin favor ofdjango-hstoreIs the library which integrates the hstore extension of PostgreSQL into Django,Compatible with:django: 1.4, 1.5, 1.6python: 2.7 y 3.3+Limitations and notesPostgreSQL’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 testsThis 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 settings.py 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.ClassesThe library provides three principal classes:djorm_hstore.fields.DictionaryFieldAn ORM field which stores a mapping of string key/value pairs in an hstore column.djorm_hstore.fields.ReferencesFieldAn ORM field which builds on DictionaryField to store a mapping of string keys to
django object references, much like ForeignKey.djorm_hstore.models.HStoreManagerAn 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)UsageInitially define some sample model:fromdjango.dbimportmodelsfromdjorm_hstore.fieldsimportDictionaryFieldfromdjorm_hstore.modelsimportHStoreManagerclassSomething(models.Model):name=models.CharField(max_length=32)data=DictionaryField(db_index=True)objects=HStoreManager()def__unicode__(self):returnself.nameYou then treat thedatafield as simply a dictionary of string pairs:instance=Something.objects.create(name='something',data={'a':'1','b':'2'})assertinstance.data['a']=='1'empty=Something.objects.create(name='empty')assertempty.data=={}empty.data['a']='1'empty.save()assertSomething.objects.get(name='something').data['a']=='1'You can issue indexed queries against hstore fields:fromdjorm_hstore.expressionsimportHstoreExpressionasHE# equivalenceSomething.objects.filter(data={'a':'1','b':'2'})# subset by key/value mappingSomething.objects.where(HE("data").contains({'a':'1'}))# subset by list of keysSomething.objects.where(HE("data").contains(['a','b']))# subset by single keySomething.objects.where(HE("data").contains("a"))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')'1'# 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.fromdjorm_hstore.functionsimportHstoreSlice,HstorePeek,HstoreKeysqueryset=SomeModel.objects.annotate_functions(sliced=HstoreSlice("hstorefield",['v']),peeked=HstorePeek("hstorefield","v"),keys=HstoreKeys("hstorefield"),)Psycopg2 hstore registrationIf for some reason you have to use djorm_hstore along databases that don’t have
hstore extension installed, you can skip hstore registration by settingHAS_HSTOREtoFalsein your database config:DATABASES={'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 createDictionaryFieldin this database.
Be sure to check outallow_syncdbdocumentation.