Django
HStoreField के साथ तार को मैप करना - एक PostgreSQL विशिष्ट क्षेत्र
खोज…
वाक्य - विन्यास
- FooModel.objects.filter (field_name__key_name = 'value to query')
HStoreField की स्थापना
सबसे पहले, हमें HStoreField काम करने के लिए कुछ सेटअप करने की आवश्यकता होगी।
- सुनिश्चित करें कि
django.contrib.postgresआपके `INSTALLED_APPS में है - अपने माइग्रेशन में
HStoreExtensionजोड़ें। किसी भीCreateModelयाAddFieldपलायन से पहलेHStoreExtensionरखना याद रखें।
from django.contrib.postgres.operations import HStoreExtension
from django.db import migrations
class FooMigration(migrations.Migration):
# put your other migration stuff here
operations = [
HStoreExtension(),
...
]
अपने मॉडल में HStoreField को जोड़ना
->नोट: सुनिश्चित करें कि आपने इस उदाहरण के साथHStoreFieldपहलेHStoreFieldपहले सेट किया है। (ऊपर)
HStoreField को आरंभ करने के लिए किसी पैरामीटर की आवश्यकता नहीं है।
from django.contrib.postgres.fields import HStoreField
from django.db import models
class Catalog(models.model):
name = models.CharField(max_length=200)
titles_to_authors = HStoreField()
एक नया मॉडल उदाहरण बनाना
create() लिए स्ट्रिंग्स के लिए एक देशी अजगर शब्दकोश मानचित्रण तार पास create() ।
Catalog.objects.create(name='Library of Congress', titles_to_authors={
'Using HStoreField with Django': 'CrazyPython and la communidad',
'Flabbergeists and thingamajigs': 'La Artista Fooista',
'Pro Git': 'Scott Chacon and Ben Straub',
})
मुख्य लुकअप करना
Catalog.objects.filter(titles__Pro_Git='Scott Chacon and Ben Straub')
का उपयोग कर शामिल हैं
एक दर्रा dict के लिए वस्तु field_name__contains एक कीवर्ड तर्क के रूप में।
Catalog.objects.filter(titles__contains={
'Pro Git': 'Scott Chacon and Ben Straub'})
SQL ऑपरेटर `@>` के बराबर है।
Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow