Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup a Postgres extension?

In the latest release of Django (1.8), a few model fields have been added to take advantage of the Postgres data types. I am interested in HStoreField and the documentation asks to setup a PG extension in order to use the new HStoreFields in the models.

How do I actually use this HStoreExtension class to perform the database extension?

like image 211
Buddyshot Avatar asked May 19 '15 16:05

Buddyshot


1 Answers

The HStoreField docs ask you to set up the extension by adding a migration.

You can create an empty migration with the command

./manage.py makemigrations yourapp --empty

In the created migration file, you can then import the extension,

django.contrib.postgres.operations import HStoreExtension

and add it to the list of operations.

operations = [
    HStoreExtension(),
]

Once you have created this migration, you can then use the HStoreField in your models.

As an example, refer to this migration file used in the Django's postgres tests. It sets up two extensions, HStoreExtension() and UnaccentExtension.

like image 191
Alasdair Avatar answered Sep 19 '22 03:09

Alasdair