如何使用由南方管理的现有应用程序设置django-hstore?

Max*_* R. 21 python django psycopg2 hstore

我尝试使用这个很好的教程使用django-hstore .我在South管理的现有应用中添加了两个类:

class Attribute(models.Model):
    name  = models.CharField(max_length=200, verbose_name=_("name"))
    description = models.CharField(max_length=1000, verbose_name=_("description"))

class Measure(models.Model):
    attribute = models.ForeignKey(Attribute)
    data = hstore.DictionaryField(db_index=True)
    objects = hstore.HStoreManager()
Run Code Online (Sandbox Code Playgroud)

做了一个schemamigration --auto,启动了迁移并得到了一个django.db.utils.DatabaseError: type "hstore" does not exist.

好吧,tuto似乎不完整,django-hstore 文档告诉我使用自定义数据库后端,我将以下内容添加到我的设置文件中:

DATABASES['default']['ENGINE'] = 'django_hstore.postgresql_psycopg2'
Run Code Online (Sandbox Code Playgroud)

后来我有一个KeyError: 'default'south/db/__init__.py", line 78.此时,intertubes +一些试验/错误将我指向SOUTH_DATABASE_ADAPTERS设置变量,我在设置中添加了以下内容:

SOUTH_DATABASE_ADAPTERS = {'default': 'south.db.postgresql_psycopg2'}
Run Code Online (Sandbox Code Playgroud)

新错误:

File ".../psycopg2/extras.py", line 769, in register_hstore
"hstore type not found in the database. "
psycopg2.ProgrammingError: hstore type not found in the database. please install it from your 'contrib/hstore.sql' file
Run Code Online (Sandbox Code Playgroud)

现在这很奇怪,因为我安装了hstore扩展:

$ sudo -u postgres psql
create extension hstore;
postgres=# CREATE EXTENSION hstore;
ERROR:  extension "hstore" already exists
postgres=# \dx
                           List of installed extensions
  Name   | Version |   Schema   |                   Description                    
---------+---------+------------+--------------------------------------------------
 hstore  | 1.0     | public     | data type for storing sets of (key, value) pairs
 plpgsql | 1.0     | pg_catalog | PL/pgSQL procedural language
(2 rows)
postgres=# SELECT 'hstore'::regtype::oid;
  oid  
-------
 57704
(1 row)
Run Code Online (Sandbox Code Playgroud)

这应该怎么样?我正在使用Django 1.4,Postgresql 9.1.

Max*_* R. 21

我最终发现没有为我使用的特定数据库安装hstore扩展:

$ psql -d mydb
psql (9.1.4)
Type "help" for help.

mydb=# SELECT t.oid, typarray FROM pg_type t JOIN pg_namespace ns ON typnamespace = ns.oid WHERE typname = 'hstore';
 oid | typarray 
-----+----------
(0 rows)

mydb=# \dx
                 List of installed extensions
  Name   | Version |   Schema   |         Description          
---------+---------+------------+------------------------------
 plpgsql | 1.0     | pg_catalog | PL/pgSQL procedural language
(1 row)

mydb=# create extension hstore;
WARNING:  => is deprecated as an operator name
DETAIL:  This name may be disallowed altogether in future versions of PostgreSQL.
CREATE EXTENSION
mydb=# \dx
                           List of installed extensions
  Name   | Version |   Schema   |                   Description                    
---------+---------+------------+--------------------------------------------------
 hstore  | 1.0     | public     | data type for storing sets of (key, value) pairs
 plpgsql | 1.0     | pg_catalog | PL/pgSQL procedural language
(2 rows)

mydb=# SELECT t.oid, typarray FROM pg_type t JOIN pg_namespace ns ON typnamespace = ns.oid WHERE typname = 'hstore';
  oid  | typarray 
-------+----------
 58800 |    58805
(1 row)
Run Code Online (Sandbox Code Playgroud)

我认为在hstore安装之后创建的数据库将包含扩展名.似乎并非如此,我是否误解了扩展如何工作?它们是特定于数据库的吗?


Cra*_*son 5

Django 现在包含一个迁移操作来hstore在 PostgreSQL 中创建扩展

from django.contrib.postgres.operations import HStoreExtension

class Migration(migrations.Migration):
    ...

    operations = [
        HStoreExtension(),
        ...
    ]
Run Code Online (Sandbox Code Playgroud)