django orm扩展包的hstore模块(在一个统一包中构建的第三方插件集合)。

djorm-ext-hstore的Python项目详细描述


这个库现在已弃用,取而代之的是django-hstore

是将postgresql的hstore扩展集成到django的库,

兼容:

  • django:1.4、1.5、1.6
  • Python:2.7年3.3+

限制和注意事项

  • postgresql的hstore实现没有类型的概念;它存储字符串键到 字符串值。此库不尝试将键或值强制为字符串。
  • 使用此软件包时不会自动安装hstore扩展。必须手动安装。(要执行测试,必须在template1数据库上安装hstore扩展。
  • 对于运行测试,必须在template1数据库上安装hstore扩展。(短版)

运行测试的限制

这个限制影响到错误11和12(不能用其他方法解决 而不是在template1数据库上安装hstore。

完整的解释,非常感谢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.

我强烈建议在template1上安装hstore,以避免出现奇怪的行为。

课程

图书馆提供三个主要类别:

djorm_hstore.fields.DictionaryField
在hstore列中存储字符串键/值对映射的orm字段。
djorm_hstore.fields.ReferencesField
一个基于dictionaryfield的form字段,用于存储字符串键到 django对象引用,非常像foreignkey。
djorm_hstore.models.HStoreManager
一个orm管理器,提供库的大部分查询功能。

注意:预定义的hstore管理器继承了djorm ext expressions模块(django orm extensions包的一部分)的所有功能

用法

最初定义一些示例模型:

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.name

然后将data字段视为字符串对的字典:

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'

您可以对hstore字段发出索引查询:

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"))

您还可以使用管理器利用一些数据库端功能:

# 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')

除了过滤器和检索键或hstore字段值的特定方法之外, 我们也可以使用注释,然后我们可以过滤它们。

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

psycopg2 hstore注册

如果出于某种原因,您必须在没有 hstore extension已安装,您可以通过设置跳过hstore注册 HAS_HSTOREFalse在数据库配置中:

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,}}

如果这样做了,那么不要试图在这个数据库中创建DictionaryField。 一定要查看allow_syncdb文档。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
在Java中使用BufferedReader类读取文本文件的子字符串   java如何在JSP页面上包含来自另一台服务器的动态JSP   使用单表策略的java持久化Hibernate继承映射   java报告状态失败达600秒。谋杀!报告hadoop的进展   java将字符串解析为形状   使用JTable的java ClassCastException?   java在Spring引导中关闭数据库   java Android Studio调试错误(Ubuntu)   java如何区分apache beam中KV实例中的两个键?   java将RealmObjectChangeListener添加到异步下载的RealmObject   java匹配模式之前的所有内容,包括新行和/或回车   java使用JAXB在XML中动态更改元素序列   java如何在MACOS中找到动态库(.dylib文件)的版本   Android中的java Nanohttpd服务器   java libGDX:3d动画不工作