मेरे आखिरी उत्तर के बाद, Django ने pre_syncdb
सिग्नल को हटा दिया और हटा दिया। मैंने हाल के संस्करणों को समायोजित करने के लिए उत्तर अपडेट किया है। मूल यांत्रिकी नए संस्करणों के लिए समान हैं क्योंकि दोनों विधियां संकेतों पर निर्भर करती हैं और SQL कोड जो केवल तभी निष्पादित करता है जब HSTORE एक्सटेंशन मौजूद न हो।
Django 1.8+
के बाद से Django डीबी माइग्रेशन शुरू की, pre_syncdb
संकेतों marked deprecated in 1.7 और completely removed in 1.9 थे। हालांकि, उन्होंने pre_migrate
नामक एक नया सिग्नल पेश किया जिसका उपयोग उसी तरह किया जा सकता है।
"""
This is an example models.py which contains all model definition.
"""
from django.dispatch import receiver
from django.db import connection, models
from django.db.models.signals import pre_syncdb
import sys
# The sender kwarg is optional but will be called for every pre_syncdb signal
# if omitted. Specifying it ensures this callback to be called once.
@receiver(pre_migrate, sender=sys.modules[__name__])
def setup_postgres_hstore(sender, **kwargs):
"""
Always create PostgreSQL HSTORE extension if it doesn't already exist
on the database before syncing the database.
Requires PostgreSQL 9.1 or newer.
"""
cursor = connection.cursor()
cursor.execute("CREATE EXTENSION IF NOT EXISTS hstore")
# ...rest of your model definition goes here
class Foo(models.Model):
# ...field definitions, etc.
Django 1.6+ (मूल जवाब)
मेरे सुझाव pre_syncdb
संकेत हुक का प्रयोग है।
अन्य उत्तर question पर मेरा उत्तर देखें।
"""
This is an example models.py which contains all model definition.
"""
from django.dispatch import receiver
from django.db import connection, models
from django.db.models.signals import pre_syncdb
import sys
# The sender kwarg is optional but will be called for every pre_syncdb signal
# if omitted. Specifying it ensures this callback to be called once.
@receiver(pre_syncdb, sender=sys.modules[__name__])
def setup_postgres_hstore(sender, **kwargs):
"""
Always create PostgreSQL HSTORE extension if it doesn't already exist
on the database before syncing the database.
Requires PostgreSQL 9.1 or newer.
"""
cursor = connection.cursor()
cursor.execute("CREATE EXTENSION IF NOT EXISTS hstore")
# ...rest of your model definition goes here
class Foo(models.Model):
# ...field definitions, etc.
pre_syncdb
संकेत से पहले मॉडल तालिका बनने निकाल दिया जाता है, यह सुनिश्चित करने के लिए आदर्श विस्तार स्थापित किया गया है जब परीक्षण डेटाबेस हर बार की स्थापना की जा रही है बना रही है। IF NOT EXISTS
यह सुनिश्चित करता है कि एक्सटेंशन पहले से स्थापित होने पर PostgreSQL कमांड को अनदेखा करता है। यदि आप पहले से मौजूद एक्सटेंशन पर CREATE EXTENSION
चलाते हैं तो आपको एक त्रुटि मिलेगी।
यह डिफ़ॉल्ट Django इकाई परीक्षण ढांचे के लिए काम करता है और सबसे अधिक संभावना Django नाक परीक्षण के लिए काम करेगा।
संकेतों पर और जानकारी: https://docs.djangoproject.com/en/1.6/ref/signals/#management-signals
आप 'template1' पोस्टग्रेज़ डेटाबेस पर एक्सटेंशन बनाकर अपने आप को बहुत दर्द बचा सकते हैं। उसके बाद आपके द्वारा बनाए गए किसी भी डेटाबेस में एचएसटीओआर एक्सटेंशन होगा। – rantanplan
rantaplan, जो जवाब होना चाहिए था! – 0atman
यह ठीक है, मैं आमतौर पर एक-पंक्ति उत्तर देने से बचता हूं। खुशी मैंने मदद की। – rantanplan