且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在现有模型上激活不重要的扩展名

更新时间:2023-12-02 22:43:52

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

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>]