Django                
            Kartlägga strängar till strängar med HStoreField - ett PostgreSQL-specifikt fält
        
        
            
    Sök…
Syntax
- FooModel.objects.filter (field_name__key_name = 'värde till fråga')
Ställa in HStoreField
 Först måste vi göra några inställningar för att få HStoreField fungera. 
-  se till att django.contrib.postgresfinns i dina INSTALLED_APPS
-  Lägg till HStoreExtensiontill dina migreringar. Kom ihåg att läggaHStoreExtensionföre någonCreateModelellerAddFieldmigrering.
from django.contrib.postgres.operations import HStoreExtension
from django.db import migrations
class FooMigration(migrations.Migration):
    # put your other migration stuff here
    operations = [
        HStoreExtension(),
        ...
    ]
Lägga till HStoreField till din modell
->Obs!HStoreFieldtill att du ställer inHStoreFieldförst innan du fortsätter med det här exemplet. (ovan)
 Inga parametrar krävs för att initiera en 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()
Skapa en ny modellinstans
 Vidarebefordra en inbyggd pythonordbok som mappar strängar till strängar för att 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',
})
Utför nyckeluppslag
Catalog.objects.filter(titles__Pro_Git='Scott Chacon and Ben Straub')
Användning innehåller
 dict ett dict objekt till field_name__contains som ett nyckelordargument. 
Catalog.objects.filter(titles__contains={
        'Pro Git': 'Scott Chacon and Ben Straub'})
Modified text is an extract of the original Stack Overflow Documentation
        Licensierat under CC BY-SA 3.0
        Inte anslutet till Stack Overflow