在Django鼻子测试中安装hstore扩展

2024-04-29 10:41:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经成功地安装了hstore扩展,当我syncdb时一切都正常。(我正在使用djorm-ext-hstore

但是,nose创建了一个新的temp数据库来运行测试,其中没有安装hstore。在

在nose同步数据库之前,我需要在testdb上运行CREATE EXTENSION HSTORE;,但是我找不到任何关于如何实现这一点的信息。在

有什么想法吗?在


Tags: 信息数据库createextensionhstoretempextdjorm
3条回答

这不是问题:解决这个问题的最佳方法是在默认数据库上应用hstore扩展template1

psql -d template1 -c 'create extension hstore;'

参考号:How to create a new database with the hstore extension already installed?

我假设你用的是django-nose。在这种情况下,您应该创建自己的TestSuiteRunner

from django.db import connections, DEFAULT_DB_ALIAS
from django_nose import NoseTestSuiteRunner

class MyTestSuiteRunner(NoseTestSuiteRunner):
    def setup_databases(self):
        result = super(MyTestSuiteRunner, self).setup_databases()

        connection = connections[DEFAULT_DB_ALIAS]
        cursor = connection.cursor()
        cursor.execute('CREATE EXTENSION HSTORE')

        return result

然后在settings.py中,您应该指定您的自定义测试运行程序:

^{pr2}$

使用Django 1.8:

from django.contrib.postgres.operations import HStoreExtension

class Migration(migrations.Migration):
    ...

    operations = [
        HStoreExtension(),
        ...
    ]

https://docs.djangoproject.com/en/1.8/ref/contrib/postgres/fields/#hstorefield

编辑:请注意还有一个JSONField,它处理(取消)json的编组和内联搜索。它不需要HStoreExtension。要求Django>;=1.11和Postgres>;=9.4。在

相关问题 更多 >