From 9306a0bb2c027deee7db0d89d5eb40989a5fcb6d Mon Sep 17 00:00:00 2001 From: sabaimran Date: Mon, 2 Sep 2024 17:35:21 -0700 Subject: [PATCH] Prefetch the settings and openai_config of a texttoimagemodelconfig --- src/khoj/database/adapters/__init__.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/khoj/database/adapters/__init__.py b/src/khoj/database/adapters/__init__.py index ec09ecac..f21f8aef 100644 --- a/src/khoj/database/adapters/__init__.py +++ b/src/khoj/database/adapters/__init__.py @@ -14,7 +14,7 @@ from apscheduler.job import Job from asgiref.sync import sync_to_async from django.contrib.sessions.backends.db import SessionStore from django.db import models -from django.db.models import Q +from django.db.models import Prefetch, Q from django.db.models.manager import BaseManager from django.db.utils import IntegrityError from django_apscheduler.models import DjangoJob, DjangoJobExecution @@ -989,7 +989,12 @@ class ConversationAdapters: @staticmethod async def aget_user_text_to_image_model(user: KhojUser) -> Optional[TextToImageModelConfig]: - config = await UserTextToImageModelConfig.objects.filter(user=user).prefetch_related("setting").afirst() + # Create a custom queryset for prefetching settings__openai_config, handling null cases + settings_prefetch = Prefetch( + "setting", queryset=TextToImageModelConfig.objects.prefetch_related("openai_config") + ) + + config = await UserTextToImageModelConfig.objects.filter(user=user).prefetch_related(settings_prefetch).afirst() if not config: default_config = await ConversationAdapters.aget_text_to_image_model_config() if not default_config: