gpt4 book ai didi

django - 如何在现有模型上激活 unaccent 扩展

转载 作者:行者123 更新时间:2023-11-29 11:15:20 25 4
gpt4 key购买 nike

当我尝试安装 unaccent Postgres 扩展(通过 postgresql-contrib 包)时,一切都按以下方式运行:

# 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 时,出现以下错误:

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。

最佳答案

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

首先,创建一个空迁移:

./manage.py makemigrations myapp --empty

然后打开文件并添加UnaccentExtensionoperations :

from django.contrib.postgres.operations import UnaccentExtension


class Migration(migrations.Migration):

dependencies = [
(<snip>)
]

operations = [
UnaccentExtension()
]

现在使用 ./manage.py migrate 应用迁移.

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

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

... 然后通过执行 postgres# ALTER ROLE <user_name> SUPERUSER; 暂时允许您的用户拥有 super 用户权限及其 NOSUPERUSER对方。 pgAdminIII 也可以做到这一点。

现在享受使用 Django 的 unaccent 功能:

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

关于django - 如何在现有模型上激活 unaccent 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31382675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com