models: Fetch "recipient" object when along with "Huddle" object.

We now fetch recipient object when querying "Huddle" object in
get_or_create_huddle_backend as this query is eventually used
to get the recipient object only in get_huddle_recipient.

This commit also updates the select_related call in the code to
populate Huddle objects in cache to pass "Recipient" as argument.
Previously no argument was passed to select_related and thus no
related objects were being fetched, with no non-null related fields
being present.
This commit is contained in:
Sahil Batra 2023-07-21 15:27:11 +05:30 committed by Tim Abbott
parent a3bb5207d2
commit 2c28b49680
2 changed files with 4 additions and 2 deletions

View File

@ -98,7 +98,7 @@ cache_fillers: Dict[
10000,
),
"huddle": (
lambda: Huddle.objects.select_related().all(),
lambda: Huddle.objects.select_related("recipient").all(),
huddle_cache_items,
3600 * 24 * 7,
10000,

View File

@ -4074,7 +4074,9 @@ def get_or_create_huddle(id_list: List[int]) -> Huddle:
)
def get_or_create_huddle_backend(huddle_hash: str, id_list: List[int]) -> Huddle:
with transaction.atomic():
(huddle, created) = Huddle.objects.get_or_create(huddle_hash=huddle_hash)
(huddle, created) = Huddle.objects.select_related("recipient").get_or_create(
huddle_hash=huddle_hash
)
if created:
recipient = Recipient.objects.create(type_id=huddle.id, type=Recipient.HUDDLE)
huddle.recipient = recipient