同时创建json索引
django-indices的Python项目详细描述
同时创建json索引
这个django应用程序提供的功能类似于 indexes, 只是它提供了一个创建索引的选项 concurrently。
目前,只支持postgres json索引。
安装
使用pip安装最新版本:
pip install django_indices
将django_indices
添加到django设置文件中的INSTALLED_APPS
:
INSTALLED_APPS=(...,"django_indices",)
运行迁移:
python manage.py migrate django_indices
将您想要的索引添加到所需模型的Meta
类中。那就跑吧
添加/删除索引的migrate_indices
管理命令:
python manage.py migrate_indices
示例
fromdjango.dbimportmodelsfromdjango.db.modelsimportQfromdjango.contrib.postgres.fieldsimportJSONFieldfromdjango_indicesimportJSONIndexclassInventory(models.Model):data=JSONField()classMeta:indices=[# Creates an index on data->'product_type'# Indexes are created concurrently by default, which doesn't lock# the table.JSONIndex(field=["data","product_type"],name="idx_inventory_product_type",),# Creates a unique index on data->'product'->>'id'## concurrently=False ensures that the index creation will execute# in a transaction. So the index is either created successfully,# or fails cleanly. This will lock the table though.JSONIndex(field=["data","product","id"],name="idx_inventory_product_id",unique=True,concurrently=False,),# Creates a partial compound index on# (data->'product'->>'brand', data->'product'->>'size')# WHERE data->>'product_type' = 'shoe' OR data->>'product_type' = 'crocs'JSONIndex(field=(["data","product","brand"],["data","product","size"]),name="idx_inventory_footwear_brand_size",where=(Q(data__product_type="shoe")|Q(data__product_type="crocs"),),]