Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 如何在Python模型迁移中生成基于函数的索引?_Django_Python 3.x_Postgresql_Indexing_Migration - Fatal编程技术网

Django 如何在Python模型迁移中生成基于函数的索引?

Django 如何在Python模型迁移中生成基于函数的索引?,django,python-3.x,postgresql,indexing,migration,Django,Python 3.x,Postgresql,Indexing,Migration,我正在使用Django、Python3.7和Postgres9.5。为了加快特定查询的速度,我想创建这个函数索引,我通常会在PostGres中这样做 CREATE INDEX my_article_idx ON article (regexp_replace(url, '\?.*$', '')) 然而,在Django和自动生成迁移的世界中,我不确定如何在models.py文件中注释我的类,以便自动生成基于函数的索引。我的模型中有问题的字段如下所示 class Article(models.Mo

我正在使用Django、Python3.7和Postgres9.5。为了加快特定查询的速度,我想创建这个函数索引,我通常会在PostGres中这样做

CREATE INDEX my_article_idx ON article (regexp_replace(url, '\?.*$', ''))
然而,在Django和自动生成迁移的世界中,我不确定如何在models.py文件中注释我的类,以便自动生成基于函数的索引。我的模型中有问题的字段如下所示

class Article(models.Model):
    ...
    url = models.TextField(default='', null=False)

您必须创建数据迁移。阅读更多关于他们的文章

步骤1-创建空数据迁移

python manage.py makemigrations --empty yourappname
步骤2-将自定义sql添加到迁移中:

# -*- coding: utf-8 -*-
# Generated by Django 1.11.14 on 2018-09-20 08:01
from __future__ import unicode_literals

from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('yourapp', '0001_name_of_depending_migration'),
    ]

    operations = [
        migrations.RunSQL(
            sql="CREATE INDEX my_article_idx ON article (regexp_replace(url, '\?.*$', ''))",
            reverse_sql='DROP INDEX my_article_idx ON article'
        )
    ]

您必须创建数据迁移。阅读更多关于他们的文章

步骤1-创建空数据迁移

python manage.py makemigrations --empty yourappname
步骤2-将自定义sql添加到迁移中:

# -*- coding: utf-8 -*-
# Generated by Django 1.11.14 on 2018-09-20 08:01
from __future__ import unicode_literals

from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('yourapp', '0001_name_of_depending_migration'),
    ]

    operations = [
        migrations.RunSQL(
            sql="CREATE INDEX my_article_idx ON article (regexp_replace(url, '\?.*$', ''))",
            reverse_sql='DROP INDEX my_article_idx ON article'
        )
    ]

您好,我的appname/migrations文件夹中已经生成了许多迁移,包括一个appname/migrations/0001_initial.py文件。鉴于此,我将如何执行您的建议?如果您担心带有“依赖项”的行,Django将在生成空迁移时正确选择迁移文件夹中的最后一次迁移。只需从步骤1运行该命令。或者你还坚持做别的什么?啊,我对那句话的作用感到困惑。但一切都像你描述的那样起作用。ThxHi,我已经在appname/migrations文件夹中生成了许多迁移,包括一个appname/migrations/0001_initial.py文件。鉴于此,我将如何执行您的建议?如果您担心带有“依赖项”的行,Django将在生成空迁移时正确选择迁移文件夹中的最后一次迁移。只需从步骤1运行该命令。或者你还坚持做别的什么?啊,我对那句话的作用感到困惑。但一切都像你描述的那样起作用。谢谢