Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

python - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...