支持PostgreSQL的Django HStore。
hstore-field的Python项目详细描述
#hstore field
hstore field与django1.5+不兼容
hstore field是一个库,它集成了postgresql到django的扩展(http://www.postgresql.org/docs/9.0/interactive/hstore.html)
postgresql 9.0+,psycopg 2.3+。
hstore字段从
[jordanm django hstore](http://github.com/jordanmdjango-hstore)和
[niwibe/django-orm扩展](https://github.com/niwibe/django-orm扩展)中获得了一些灵感,
但是它使用了完全不同的机制对于扩展django,它具有以下优点:
1。不需要自定义数据库后端(代价是不支持hstore字段上的
索引)
1。不需要自定义QuerySet类,使其与
Geodjango或任何其他执行QuerySet子类
1的扩展完全兼容。支持查询中的范围查找类型(例如,``uu lt`,``uu gt`,等等…)
1。主要与South兼容(具体请参见下面的限制)
hstore field将在您向字段写入
时将数字和日期转换为字符串,但当从数据库中检索到hstore字典时,它*不会将它们*转换回原始类型。您可以将自定义的
类序列化到hstore,方法是给它一个'to-hstore'方法,该方法必须返回
一个字符串。
-hstore字段将使用'connection-created'信号自动尝试在您连接到的任何
数据库上安装configure hstore。如果将
连接到多个数据库,则可能会出现问题。
-使用south将具有"null=false"的hstorefield添加到现有模型中是有问题的,因为south无法为默认值发出正确的sql。一种解决方法是通过将sql直接放入迁移中来添加列
``python
def forwards(self,orm):
db.execute('alter table"[table]"add column"[column]"hstore not null default hstore(array[]::varchar[]);')
``
另一种方法是添加带有"null=true"的字段,填充该字段,
然后设置"null=false"。这实际上通常被认为是良好的实践,
,因为默认值可能会导致意外的问题。
hstore必须安装在postgresql contrib文件夹中
1。如果您运行的是postgresql 9.0,那么包含"pg廑config"
的目录必须在您的"path"上
\usage
模型定义很简单:
from django.db import models
from hstore廑field import fields
class item(models.model):
name=models.charfield(最大长度=64)
data=fields.hstorefield()
````
'b':'2'})
assert instance.data['a']='1'
empty=item.objects.create(name='empty')
assert empty.data=={}
empty.data['a']='1'
empty.save()
assert item.objects.get(name='something').data['a']='1'
```
使用'hq'类(类似于'q'类)
``python
from hstore\u field.query import hq
元组)
item.objects.filter(hq(数据包含=['a','b'])
````
NST hstore值:
`` python
'2']))
item.objects.filter(hq(data_uuu a_uu a_u a_u a_u a_u a_uu lt=1))
item.objects.filter(hq(data_u a_u a_u a_u a_u a_u a_u a_u a_u a_u a_u a_u lt=1.1))
item.objects.filter(bq(data 1号,1,0,15))
使用非字符串
类型的键,该键的任何非空值如果不能强制转换为适当的
类型,将导致查询失败。
"hq"对象可以像"q"对象一样,使用`&;`、` `和` ~ `组合。但
它们只能与其他"hq"对象组合,而不能与任何"q"对象组合。
若要将"hq"对象与"q"对象组合,必须首先将"hq"对象包装到"q"对象中。例如:
`` python
item.objects.filter(hq(data_u a_u lt=10)&hq(data_u b_u lt=20))是!
item.objects.filter(q(hq(data_u a_u lt=10))&q(data_u name="foo"))是的!
item.objects.filter(hq(data_u a_u lt=10)&q(data_u name="foo"))否!
```
range查询不是特别快,因为它们需要表扫描,并且需要将每个记录的hstore->;键从字符串转换为另一种类型。但是,
它比将整个表作为
django模型对象发送到应用程序层并在那里过滤它们快得多(在有限的测试中,速度快了3-6倍)。
hstore field与django1.5+不兼容
hstore field是一个库,它集成了postgresql到django的扩展(http://www.postgresql.org/docs/9.0/interactive/hstore.html)
postgresql 9.0+,psycopg 2.3+。
hstore字段从
[jordanm django hstore](http://github.com/jordanmdjango-hstore)和
[niwibe/django-orm扩展](https://github.com/niwibe/django-orm扩展)中获得了一些灵感,
但是它使用了完全不同的机制对于扩展django,它具有以下优点:
1。不需要自定义数据库后端(代价是不支持hstore字段上的
索引)
1。不需要自定义QuerySet类,使其与
Geodjango或任何其他执行QuerySet子类
1的扩展完全兼容。支持查询中的范围查找类型(例如,``uu lt`,``uu gt`,等等…)
1。主要与South兼容(具体请参见下面的限制)
hstore field将在您向字段写入
时将数字和日期转换为字符串,但当从数据库中检索到hstore字典时,它*不会将它们*转换回原始类型。您可以将自定义的
类序列化到hstore,方法是给它一个'to-hstore'方法,该方法必须返回
一个字符串。
-hstore字段将使用'connection-created'信号自动尝试在您连接到的任何
数据库上安装configure hstore。如果将
连接到多个数据库,则可能会出现问题。
-使用south将具有"null=false"的hstorefield添加到现有模型中是有问题的,因为south无法为默认值发出正确的sql。一种解决方法是通过将sql直接放入迁移中来添加列
``python
def forwards(self,orm):
db.execute('alter table"[table]"add column"[column]"hstore not null default hstore(array[]::varchar[]);')
``
另一种方法是添加带有"null=true"的字段,填充该字段,
然后设置"null=false"。这实际上通常被认为是良好的实践,
,因为默认值可能会导致意外的问题。
hstore必须安装在postgresql contrib文件夹中
1。如果您运行的是postgresql 9.0,那么包含"pg廑config"
的目录必须在您的"path"上
\usage
模型定义很简单:
from hstore廑field import fields
class item(models.model):
name=models.charfield(最大长度=64)
data=fields.hstorefield()
````
'b':'2'})
assert instance.data['a']='1'
empty=item.objects.create(name='empty')
assert empty.data=={}
empty.data['a']='1'
empty.save()
assert item.objects.get(name='something').data['a']='1'
```
使用'hq'类(类似于'q'类)
``python
from hstore\u field.query import hq
元组)
item.objects.filter(hq(数据包含=['a','b'])
````
NST hstore值:
`` python
'2']))
item.objects.filter(hq(data_uuu a_uu a_u a_u a_u a_u a_uu lt=1))
item.objects.filter(hq(data_u a_u a_u a_u a_u a_u a_u a_u a_u a_u a_u a_u lt=1.1))
item.objects.filter(bq(data 1号,1,0,15))
使用非字符串
类型的键,该键的任何非空值如果不能强制转换为适当的
类型,将导致查询失败。
"hq"对象可以像"q"对象一样,使用`&;`、` `和` ~ `组合。但
它们只能与其他"hq"对象组合,而不能与任何"q"对象组合。
若要将"hq"对象与"q"对象组合,必须首先将"hq"对象包装到"q"对象中。例如:
`` python
item.objects.filter(hq(data_u a_u lt=10)&hq(data_u b_u lt=20))是!
item.objects.filter(q(hq(data_u a_u lt=10))&q(data_u name="foo"))是的!
item.objects.filter(hq(data_u a_u lt=10)&q(data_u name="foo"))否!
```
range查询不是特别快,因为它们需要表扫描,并且需要将每个记录的hstore->;键从字符串转换为另一种类型。但是,
它比将整个表作为
django模型对象发送到应用程序层并在那里过滤它们快得多(在有限的测试中,速度快了3-6倍)。