Skip to content

Commit

Permalink
Prefetch the settings and openai_config of a texttoimagemodelconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
sabaimran committed Sep 3, 2024
1 parent 132eac0 commit 9306a0b
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/khoj/database/adapters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
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
Expand Down Expand Up @@ -989,7 +989,12 @@ def get_user_text_to_image_model_config(user: KhojUser):

@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:
Expand Down

0 comments on commit 9306a0b

Please sign in to comment.