5

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?

Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
Buddyshot
  • 1,614
  • 1
  • 17
  • 44

1 Answers1

6

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.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • I have a handful of apps in my project, not all of them will use `HStoreField`. Should I create the migration file with `./manage.py makemigrations app1 app2 app3 app4 --empty`, or the app provided at the command line does not really matter since the operation affects the whole database? Thanks already for your support. – Buddyshot May 19 '15 at 16:37
  • 2
    `makemigrations` takes a single app name (e.g. `yourapp` in my answer above), not multiple app names. I suspect that you only need to create the migration for one app, and not every app where you use `HStoreField`. However you will have to test this as I have not tried it. – Alasdair May 19 '15 at 16:43