from django.contrib.postgres.indexes import GinIndex from django.db import models class MyModel(models.Model): my_json_field = models.JSONField() my_array_field = models.ArrayField(models.IntegerField()) class Meta: indexes = [ GinIndex(fields=['my_json_field']), GinIndex(fields=['my_array_field']), ]
# get all instances where the JSON field contains a specific value MyModel.objects.filter(my_json_field__contains={'my_key': 'my_value'}) # get all instances where the array field contains a specific value MyModel.objects.filter(my_array_field__contains=[1])Overall, GinIndex is a useful tool for optimizing search queries on certain data types in PostgreSQL databases.