5
votes

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?

1

1 Answers

6
votes

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.