如何设置Postgres扩展? [英] How to setup a Postgres extension?
问题描述
在最新版本的Django(1.8)中,添加了几个模型字段来利用Postgres数据类型。我对 HStoreField 感兴趣,文档要求设置一个PG 扩展程序,以便使用新的
如何实际使用这个 HStoreExtension
类执行数据库扩展?
HStoreField
docs ask您可以通过添加迁移来设置扩展。
您可以使用命令
创建一个空迁移 ./ manage.py makemigrations yourapp --empty
在创建迁移文件,然后可以导入扩展名
django.contrib.postgres.operations import HStoreExtension
并将其添加到操作列表中。
operations = [
HStoreExtension(),
]
创建此迁移后,您可以使用您的模型中的HStoreField
。
举个例子,请参考 HStoreExtension()和 UnaccentExtension
。
In the latest release of Django (1.8), a few model fields have been added to take advantage of the Postgres data types. I am interested in HStoreField and the documentation asks to setup a PG extension in order to use the new HStoreField
s in the models.
How do I actually use this HStoreExtension
class to perform the database extension?
The HStoreField
docs ask you to set up the extension by adding a migration.
You can create an empty migration with the command
./manage.py makemigrations yourapp --empty
In the created migration file, you can then import the extension,
django.contrib.postgres.operations import HStoreExtension
and add it to the list of operations.
operations = [
HStoreExtension(),
]
Once you have created this migration, you can then use the HStoreField
in your models.
As an example, refer to this migration file used in the Django's postgres tests. It sets up two extensions, HStoreExtension()
and UnaccentExtension
.
这篇关于如何设置Postgres扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!