﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
25454	Module level database queries in a project prevent register_hstore from being called	Alessandro Reichert	Claude Paroz <claude@…>	"Using hstorefield through the webserver works fine, but for applications that don't use the webserver like management commands we got a problem with models that use hstorefield. Among these problems is retrieving the hstorefield object which is retrieved as 'str' insteado of a 'dict'.

I found out a workaround that may be the cause of the bug but I've not enought knowledge to propose a fix.

Here is how to replicate the problem:


{{{
# subaccount is a hstorefield of AccountEntry model

In [8]: entry = AccountEntry.objects.get(pk=1)

In [9]: entry.subaccount
Out[9]: '""store""=>""velocity""'
In [10]: entry.subaccount.__class__
Out[10]: str
}}}

To get it working I'd to do this trick:
{{{
In [12]: from psycopg2.extras import register_hstore

In [13]: from django.db import connection

In [14]: register_hstore(connection.connection, globally=True, unicode=True)

In [15]: entry.subaccount
Out[15]: '""store""=>""velocity""'

In [16]: entry2 = AccountEntry.objects.get(pk=2)

In [17]: entry2.subaccount
Out[17]: {u'store': u'velocity'}

In [18]: entry2.subaccount.__class__
Out[18]: dict
}}}

Note that:
1) We are using django 1.8.4, python 2.7.9, psycopg2 2.6.1, postgresql 9.4.4
2) We installed the hstore extension in the template1 and we have created the django database after the hstore extension creation.
3) We have in our INSTALLED_APPS the django.contrib.postresql

Thanks!"	Cleanup/optimization	closed	contrib.postgres	dev	Normal	fixed	hstorefield	craig.ds@…	Ready for checkin	1	0	0	0	0	0
