From 89768fa02e4fc5da9054eec8ce495c069ab8ffdb Mon Sep 17 00:00:00 2001 From: Sayyid Hamid Mahdavi Date: Sat, 15 Nov 2025 14:50:18 +0330 Subject: [PATCH] index for jsonfield --- ...promotions_event_data_bc656abc_and_more.py | 37 ++++++++++++++++ apps/promotions/models.py | 44 +++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 apps/promotions/migrations/0006_event_promotions_event_data_bc656abc_and_more.py diff --git a/apps/promotions/migrations/0006_event_promotions_event_data_bc656abc_and_more.py b/apps/promotions/migrations/0006_event_promotions_event_data_bc656abc_and_more.py new file mode 100644 index 0000000..496670f --- /dev/null +++ b/apps/promotions/migrations/0006_event_promotions_event_data_bc656abc_and_more.py @@ -0,0 +1,37 @@ +# Generated by Django 5.1.7 on 2025-11-15 11:19 + +import django.contrib.postgres.indexes +from django.conf import settings +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('promotions', '0005_alter_event_user'), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + migrations.swappable_dependency(settings.OAUTH2_PROVIDER_APPLICATION_MODEL), + ] + + operations = [ + migrations.AddIndex( + model_name='event', + index=django.contrib.postgres.indexes.GinIndex(fields=['data'], name='promotions_event_data_bc656abc'), + ), + migrations.AddIndex( + model_name='plan', + index=django.contrib.postgres.indexes.GinIndex(fields=['event_list'], name='promotions_plan_event_783bb033'), + ), + migrations.AddIndex( + model_name='plan', + index=django.contrib.postgres.indexes.GinIndex(fields=['data'], name='promotions_plan_data_b632bc41'), + ), + migrations.AddIndex( + model_name='promotion', + index=django.contrib.postgres.indexes.GinIndex(fields=['data'], name='promotions_promotion_cb76da3e'), + ), + migrations.AddIndex( + model_name='recipient', + index=django.contrib.postgres.indexes.GinIndex(fields=['data'], name='promotions_recipient_fba22446'), + ), + ] diff --git a/apps/promotions/models.py b/apps/promotions/models.py index eb174cc..e349e01 100644 --- a/apps/promotions/models.py +++ b/apps/promotions/models.py @@ -1,6 +1,7 @@ import uuid from django.conf import settings +from django.contrib.postgres.indexes import GinIndex from django.db import models, transaction from django.db.models import F from django.utils import timezone @@ -67,6 +68,15 @@ class Event(BaseModel): objects = EventQuerySet.as_manager() + class Meta: + indexes = [ + GinIndex( + fields=['data'], + # hashlib.md5("promotions_event_data_GinIndex".encode()).hexdigest()[:8] used for hash + name='promotions_event_data_bc656abc' + ), + ] + def __str__(self): return self.label @@ -101,6 +111,20 @@ class Plan(BaseModel): objects = PlanQuerySet.as_manager() + class Meta: + indexes = [ + GinIndex( + fields=['event_list'], + # hashlib.md5("promotions_plan_event_list_GinIndex".encode()).hexdigest()[:8] used for hash + name='promotions_plan_event_783bb033' + ), + GinIndex( + fields=['data'], + # hashlib.md5("promotions_plan_data_GinIndex".encode()).hexdigest()[:8] used for hash + name='promotions_plan_data_b632bc41' + ), + ] + def __str__(self): return self.title @@ -133,6 +157,16 @@ class Recipient(BaseModel): data = models.JSONField(null=True, blank=True, default=dict) + class Meta: + indexes = [ + GinIndex( + fields=['data'], + # hashlib.md5("promotions_recipient_data_GinIndex".encode()).hexdigest()[:8] used for hash + name='promotions_recipient_fba22446' + ), + ] + + def __str__(self): return f"{self.label} --> {self.plan}" @@ -259,6 +293,16 @@ class Promotion(BaseModel): objects = PromotionQuerySet.as_manager() + class Meta: + indexes = [ + GinIndex( + fields=['data'], + # hashlib.md5("promotions_promotion_data_GinIndex".encode()).hexdigest()[:8] used for hash + name='promotions_promotion_cb76da3e' + ), + ] + + def change_state(self, from_states: list | tuple, to_state, same_ok=True, raise_exception=False, error_message=None, data: None | dict = None):