如何在现有模型上激活不重要的扩展名 [英] How can I activate the unaccent extension on an already existing model

查看:103
本文介绍了如何在现有模型上激活不重要的扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试安装 unaccent Postgres扩展名(通过 postgresql-contrib 软件包)时,一切正常每个以下内容:

When I try to install the unaccent Postgres extension (through the postgresql-contrib package), everything works as per the below:

# psql -U postgres -W -h localhost
Password for user postgres:
psql (9.3.9)
SSL connection (cipher: DHE-RSA-AES256-GCM-SHA384, bits: 256)
Type "help" for help.

postgres=# CREATE EXTENSION unaccent;
CREATE EXTENSION
postgres=# SELECT unaccent('Hélène');
 unaccent
----------
 Helene
(1 row)

但是,当我尝试使用Django 1.8时,出现以下错误:

However, when I try to use with Django 1.8, I get following error:

ProgrammingError: function unaccent(character varying) does not exist
LINE 1: ...able" WHERE ("my_table"."live" = true AND UNACCENT("...
                                                             ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

使用Postgresql 9.3和Django 1.8。

Using Postgresql 9.3 and Django 1.8.

推荐答案

需要手动制作和应用迁移文件。

A migration file needs to be manually made and applied.

首先,创建一个空迁移:

First, create an empty migration:

./manage.py makemigrations myapp --empty

然后打开文件并将 UnaccentExtension 添加到操作

Then open the file and add UnaccentExtension to operations:

from django.contrib.postgres.operations import UnaccentExtension


class Migration(migrations.Migration):

    dependencies = [
        (<snip>)
    ]

    operations = [
        UnaccentExtension()
    ]

现在使用 ./ manage.py migration 来应用迁移。

如果在最后一步中出现以下错误:

If you'd get following error during that last step:

django.db.utils.ProgrammingError: permission denied to create extension "unaccent"
HINT:  Must be superuser to create this extension.

...然后通过执行 postgres#临时为您的用户授予超级用户权限ALTER ROLE<用户名> SUPERUSER; 及其对应的 NOSUPERUSER 。 pgAdminIII也可以做到这一点。

... then temporarily allow superuser rights to your user by performing postgres# ALTER ROLE <user_name> SUPERUSER; and its NOSUPERUSER counterpart. pgAdminIII can do this, too.

现在可以使用Django享受令人赞叹的功能:

Now enjoy the unaccent functionality using Django:

>>> Person.objects.filter(first_name__unaccent=u"Helène")
[<Person: Michels Hélène>]

这篇关于如何在现有模型上激活不重要的扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆