How can I activate the unaccent extension on an already existing model

前端 未结 1 1524
面向向阳花
面向向阳花 2021-02-02 15:07

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

# ps         


        
相关标签:
1条回答
  • 2021-02-02 15:56

    A migration file needs to be manually made and applied.

    First, create an empty migration:

    ./manage.py makemigrations myapp --empty
    

    Then open the file and add UnaccentExtension to operations:

    from django.contrib.postgres.operations import UnaccentExtension
    
    
    class Migration(migrations.Migration):
    
        dependencies = [
            (<snip>)
        ]
    
        operations = [
            UnaccentExtension()
        ]
    

    Now apply the migration using ./manage.py migrate.

    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.
    

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

    Now enjoy the unaccent functionality using Django:

    >>> Person.objects.filter(first_name__unaccent=u"Helène")
    [<Person: Michels Hélène>]
    
    0 讨论(0)
提交回复
热议问题