Django                
            Mapper des chaînes à des chaînes avec HStoreField - un champ spécifique à PostgreSQL
        
        
            
    Recherche…
Syntaxe
- FooModel.objects.filter (nom_zone__nom_key = 'valeur à interroger')
Configuration de HStoreField
 Tout d'abord, nous devrons faire quelques réglages pour que HStoreField fonctionne. 
-  assurez-vous que django.contrib.postgresest dans votre `INSTALLED_APPS
-  Ajoutez HStoreExtensionà vos migrations. N'oubliez pas de placerHStoreExtensionavant toute migrationCreateModelouAddField.
from django.contrib.postgres.operations import HStoreExtension
from django.db import migrations
class FooMigration(migrations.Migration):
    # put your other migration stuff here
    operations = [
        HStoreExtension(),
        ...
    ]
Ajout de HStoreField à votre modèle
->Remarque: assurez-vous de configurerHStoreFieldavant de continuer avec cet exemple. (au dessus)
 Aucun paramètre n'est requis pour initialiser un 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()
Créer une nouvelle instance de modèle
 Passer un dictionnaire Python natif mappant des chaînes de caractères à 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',
})
Effectuer des recherches de clés
Catalog.objects.filter(titles__Pro_Git='Scott Chacon and Ben Straub')
Utiliser contient
 field_name__contains un objet dict à field_name__contains comme argument de mot clé. 
Catalog.objects.filter(titles__contains={
        'Pro Git': 'Scott Chacon and Ben Straub'})
Modified text is an extract of the original Stack Overflow Documentation
        Sous licence CC BY-SA 3.0
        Non affilié à Stack Overflow