Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Implement read replicas in all dataloaders (#8751)

Filip Owczarekf5bdda6f8a9d
diff --git a/saleor/graphql/account/dataloaders.py b/saleor/graphql/account/dataloaders.py
index 4cb24a6623..256d3e314d 100644
--- a/saleor/graphql/account/dataloaders.py
+++ b/saleor/graphql/account/dataloaders.py
@@ -8,7 +8,7 @@ class AddressByIdLoader(DataLoader):
context_key = "address_by_id"
def batch_load(self, keys):
- address_map = Address.objects.in_bulk(keys)
+ address_map = Address.objects.using(self.database_connection_name).in_bulk(keys)
return [address_map.get(address_id) for address_id in keys]
@@ -16,7 +16,7 @@ class UserByUserIdLoader(DataLoader):
context_key = "user_by_id"
def batch_load(self, keys):
- user_map = User.objects.in_bulk(keys)
+ user_map = User.objects.using(self.database_connection_name).in_bulk(keys)
return [user_map.get(user_id) for user_id in keys]
@@ -24,7 +24,9 @@ class CustomerEventsByUserLoader(DataLoader):
context_key = "customer_events_by_user"
def batch_load(self, keys):
- events = CustomerEvent.objects.filter(user_id__in=keys)
+ events = CustomerEvent.objects.using(self.database_connection_name).filter(
+ user_id__in=keys
+ )
events_by_user_map = defaultdict(list)
for event in events:
events_by_user_map[event.user_id].append(event)
diff --git a/saleor/graphql/app/dataloaders.py b/saleor/graphql/app/dataloaders.py
index 7f958b75bd..edddc24572 100644
--- a/saleor/graphql/app/dataloaders.py
+++ b/saleor/graphql/app/dataloaders.py
@@ -8,7 +8,7 @@ class AppByIdLoader(DataLoader):
context_key = "app_by_id"
def batch_load(self, keys):
- apps = App.objects.in_bulk(keys)
+ apps = App.objects.using(self.database_connection_name).in_bulk(keys)
return [apps.get(key) for key in keys]
@@ -16,7 +16,9 @@ class AppExtensionByIdLoader(DataLoader):
context_key = "app_extension_by_id"
def batch_load(self, keys):
- extensions = AppExtension.objects.in_bulk(keys)
+ extensions = AppExtension.objects.using(self.database_connection_name).in_bulk(
+ keys
+ )
return [extensions.get(key) for key in keys]
@@ -24,7 +26,9 @@ class AppExtensionByAppIdLoader(DataLoader):
context_key = "app_extension_by_app_id"
def batch_load(self, keys):
- extensions = AppExtension.objects.filter(app_id__in=keys)
+ extensions = AppExtension.objects.using(self.database_connection_name).filter(
+ app_id__in=keys
+ )
extensions_map = defaultdict(list)
app_extension_loader = AppExtensionByIdLoader(self.context)
for extension in extensions.iterator():
diff --git a/saleor/graphql/channel/dataloaders.py b/saleor/graphql/channel/dataloaders.py
index 7d80f07268..a4e1ff075e 100644
--- a/saleor/graphql/channel/dataloaders.py
+++ b/saleor/graphql/channel/dataloaders.py
@@ -15,7 +15,7 @@ class ChannelByIdLoader(DataLoader):
context_key = "channel_by_id"
def batch_load(self, keys):
- channels = Channel.objects.in_bulk(keys)
+ channels = Channel.objects.using(self.database_connection_name).in_bulk(keys)
return [channels.get(channel_id) for channel_id in keys]
@@ -77,8 +77,14 @@ class ChannelWithHasOrdersByIdLoader(DataLoader):
context_key = "channel_with_has_orders_by_id"
def batch_load(self, keys):
- orders = Order.objects.filter(channel=OuterRef("pk"))
- channels = Channel.objects.annotate(has_orders=Exists(orders)).in_bulk(keys)
+ orders = Order.objects.using(self.database_connection_name).filter(
+ channel=OuterRef("pk")
+ )
+ channels = (
+ Channel.objects.using(self.database_connection_name)
+ .annotate(has_orders=Exists(orders))
+ .in_bulk(keys)
+ )
return [channels.get(channel_id) for channel_id in keys]
@@ -86,9 +92,11 @@ class ShippingZonesByChannelIdLoader(DataLoader):
context_key = "shippingzone_by_channel"
def batch_load(self, keys):
- zone_and_channel_is_pairs = ShippingZone.objects.filter(
- channels__id__in=keys
- ).values_list("pk", "channels__id")
+ zone_and_channel_is_pairs = (
+ ShippingZone.objects.using(self.database_connection_name)
+ .filter(channels__id__in=keys)
+ .values_list("pk", "channels__id")
+ )
channel_shipping_zone_map = defaultdict(list)
for zone_id, channel_id in zone_and_channel_is_pairs:
channel_shipping_zone_map[channel_id].append(zone_id)
diff --git a/saleor/graphql/checkout/dataloaders.py b/saleor/graphql/checkout/dataloaders.py
index 4c82daad69..ea87f33e2d 100644
--- a/saleor/graphql/checkout/dataloaders.py
+++ b/saleor/graphql/checkout/dataloaders.py
@@ -30,7 +30,11 @@ class CheckoutByTokenLoader(DataLoader):
context_key = "checkout_by_token"
def batch_load(self, keys):
- checkouts = Checkout.objects.filter(token__in=keys).in_bulk()
+ checkouts = (
+ Checkout.objects.using(self.database_connection_name)
+ .filter(token__in=keys)
+ .in_bulk()
+ )
return [checkouts.get(token) for token in keys]
@@ -116,7 +120,7 @@ class CheckoutByIdLoader(DataLoader):
context_key = "checkout_by_id"
def batch_load(self, keys):
- checkouts = Checkout.objects.in_bulk(keys)
+ checkouts = Checkout.objects.using(self.database_connection_name).in_bulk(keys)
return [checkouts.get(checkout_id) for checkout_id in keys]
@@ -124,7 +128,9 @@ class CheckoutByUserLoader(DataLoader):
context_key = "checkout_by_user"
def batch_load(self, keys):
- checkouts = Checkout.objects.filter(user_id__in=keys, channel__is_active=True)
+ checkouts = Checkout.objects.using(self.database_connection_name).filter(
+ user_id__in=keys, channel__is_active=True
+ )
checkout_by_user_map = defaultdict(list)
for checkout in checkouts:
checkout_by_user_map[checkout.user_id].append(checkout)
@@ -137,11 +143,15 @@ class CheckoutByUserAndChannelLoader(DataLoader):
def batch_load(self, keys):
user_ids = [key[0] for key in keys]
channel_slugs = [key[1] for key in keys]
- checkouts = Checkout.objects.filter(
- user_id__in=user_ids,
- channel__slug__in=channel_slugs,
- channel__is_active=True,
- ).annotate(channel_slug=F("channel__slug"))
+ checkouts = (
+ Checkout.objects.using(self.database_connection_name)
+ .filter(
+ user_id__in=user_ids,
+ channel__slug__in=channel_slugs,
+ channel__is_active=True,
+ )
+ .annotate(channel_slug=F("channel__slug"))
+ )
checkout_by_user_and_channel_map = defaultdict(list)
for checkout in checkouts:
key = (checkout.user_id, checkout.channel_slug)
@@ -296,7 +306,9 @@ class CheckoutLineByIdLoader(DataLoader):
context_key = "checkout_line_by_id"
def batch_load(self, keys):
- checkout_lines = CheckoutLine.objects.in_bulk(keys)
+ checkout_lines = CheckoutLine.objects.using(
+ self.database_connection_name
+ ).in_bulk(keys)
return [checkout_lines.get(line_id) for line_id in keys]
@@ -304,7 +316,9 @@ class CheckoutLinesByCheckoutTokenLoader(DataLoader):
context_key = "checkoutlines_by_checkout"
def batch_load(self, keys):
- lines = CheckoutLine.objects.filter(checkout_id__in=keys)
+ lines = CheckoutLine.objects.using(self.database_connection_name).filter(
+ checkout_id__in=keys
+ )
line_map = defaultdict(list)
for line in lines.iterator():
line_map[line.checkout_id].append(line)
diff --git a/saleor/graphql/discount/dataloaders.py b/saleor/graphql/discount/dataloaders.py
index 2ef57fb46d..2c2353fa7f 100644
--- a/saleor/graphql/discount/dataloaders.py
+++ b/saleor/graphql/discount/dataloaders.py
@@ -25,7 +25,11 @@ class DiscountsByDateTimeLoader(DataLoader):
def batch_load(self, keys):
sales_map = {
- datetime: list(Sale.objects.active(datetime).order_by("id"))
+ datetime: list(
+ Sale.objects.using(self.database_connection_name)
+ .active(datetime)
+ .order_by("id")
+ )
for datetime in keys
}
pks = {s.pk for d, ss in sales_map.items() for s in ss}
@@ -57,9 +61,11 @@ class SaleChannelListingBySaleIdAndChanneSlugLoader(DataLoader):
def batch_load(self, keys):
sale_ids = [key[0] for key in keys]
channel_slugs = [key[1] for key in keys]
- sale_channel_listings = SaleChannelListing.objects.filter(
- sale_id__in=sale_ids, channel__slug__in=channel_slugs
- ).annotate(channel_slug=F("channel__slug"))
+ sale_channel_listings = (
+ SaleChannelListing.objects.using(self.database_connection_name)
+ .filter(sale_id__in=sale_ids, channel__slug__in=channel_slugs)
+ .annotate(channel_slug=F("channel__slug"))
+ )
sale_channel_listings_by_sale_and_channel_map = {}
for sale_channel_listing in sale_channel_listings:
key = (sale_channel_listing.sale_id, sale_channel_listing.channel_slug)
@@ -71,7 +77,9 @@ class SaleChannelListingBySaleIdLoader(DataLoader):
context_key = "salechannelisting_by_sale"
def batch_load(self, keys):
- sale_channel_listings = SaleChannelListing.objects.filter(sale_id__in=keys)
+ sale_channel_listings = SaleChannelListing.objects.using(
+ self.database_connection_name
+ ).filter(sale_id__in=keys)
sale_channel_listings_by_sale_map = defaultdict(list)
for sale_channel_listing in sale_channel_listings:
sale_channel_listings_by_sale_map[sale_channel_listing.sale_id].append(
@@ -84,7 +92,7 @@ class VoucherByIdLoader(DataLoader):
context_key = "voucher_by_id"
def batch_load(self, keys):
- vouchers = Voucher.objects.in_bulk(keys)
+ vouchers = Voucher.objects.using(self.database_connection_name).in_bulk(keys)
return [vouchers.get(voucher_id) for voucher_id in keys]
@@ -94,9 +102,11 @@ class VoucherChannelListingByVoucherIdAndChanneSlugLoader(DataLoader):
def batch_load(self, keys):
voucher_ids = [key[0] for key in keys]
channel_slugs = [key[1] for key in keys]
- voucher_channel_listings = VoucherChannelListing.objects.filter(
- voucher_id__in=voucher_ids, channel__slug__in=channel_slugs
- ).annotate(channel_slug=F("channel__slug"))
+ voucher_channel_listings = (
+ VoucherChannelListing.objects.using(self.database_connection_name)
+ .filter(voucher_id__in=voucher_ids, channel__slug__in=channel_slugs)
+ .annotate(channel_slug=F("channel__slug"))
+ )
voucher_channel_listings_by_voucher_and_channel_map = {}
for voucher_channel_listing in voucher_channel_listings:
key = (
@@ -115,9 +125,9 @@ class VoucherChannelListingByVoucherIdLoader(DataLoader):
context_key = "voucherchannellisting_by_voucher"
def batch_load(self, keys):
- voucher_channel_listings = VoucherChannelListing.objects.filter(
- voucher_id__in=keys
- )
+ voucher_channel_listings = VoucherChannelListing.objects.using(
+ self.database_connection_name
+ ).filter(voucher_id__in=keys)
voucher_channel_listings_by_voucher_map = defaultdict(list)
for voucher_channel_listing in voucher_channel_listings:
voucher_channel_listings_by_voucher_map[
@@ -132,7 +142,9 @@ class OrderDiscountsByOrderIDLoader(DataLoader):
context_key = "orderdiscounts_by_order_id"
def batch_load(self, keys):
- discounts = OrderDiscount.objects.filter(order_id__in=keys)
+ discounts = OrderDiscount.objects.using(self.database_connection_name).filter(
+ order_id__in=keys
+ )
discount_map = defaultdict(list)
for discount in discounts:
discount_map[discount.order_id].append(discount)
diff --git a/saleor/graphql/giftcard/dataloaders.py b/saleor/graphql/giftcard/dataloaders.py
index a57987d5f7..d9918a6acc 100644
--- a/saleor/graphql/giftcard/dataloaders.py
+++ b/saleor/graphql/giftcard/dataloaders.py
@@ -8,7 +8,9 @@ class GiftCardsByUserLoader(DataLoader):
context_key = "gift_cards_by_user"
def batch_load(self, keys):
- gift_cards = GiftCard.objects.filter(used_by_id__in=keys)
+ gift_cards = GiftCard.objects.using(self.database_connection_name).filter(
+ used_by_id__in=keys
+ )
gift_cards_by_user_map = defaultdict(list)
for gift_card in gift_cards:
gift_cards_by_user_map[gift_card.used_by_id].append(gift_card)
@@ -19,7 +21,9 @@ class GiftCardEventsByGiftCardIdLoader(DataLoader):
context_key = "giftcardevents_by_giftcard"
def batch_load(self, keys):
- events = GiftCardEvent.objects.filter(gift_card_id__in=keys)
+ events = GiftCardEvent.objects.using(self.database_connection_name).filter(
+ gift_card_id__in=keys
+ )
events_map = defaultdict(list)
for event in events.iterator():
events_map[event.gift_card_id].append(event)
@@ -30,13 +34,15 @@ class GiftCardTagsByGiftCardIdLoader(DataLoader):
context_key = "giftcardtags_by_giftcard"
def batch_load(self, keys):
- gift_card_gift_card_tags = GiftCard.tags.through.objects.filter(
- giftcard_id__in=keys
- )
+ gift_card_gift_card_tags = GiftCard.tags.through.objects.using(
+ self.database_connection_name
+ ).filter(giftcard_id__in=keys)
tags_ids = [
gift_card_tag.giftcardtag_id for gift_card_tag in gift_card_gift_card_tags
]
- tags = GiftCardTag.objects.in_bulk(tags_ids)
+ tags = GiftCardTag.objects.using(self.database_connection_name).in_bulk(
+ tags_ids
+ )
tags_map = defaultdict(list)
for gift_card_tag in gift_card_gift_card_tags:
tags_map[gift_card_tag.giftcard_id].append(
diff --git a/saleor/graphql/menu/dataloaders.py b/saleor/graphql/menu/dataloaders.py
index 3b41397b75..cf0285c0db 100644
--- a/saleor/graphql/menu/dataloaders.py
+++ b/saleor/graphql/menu/dataloaders.py
@@ -8,7 +8,7 @@ class MenuByIdLoader(DataLoader):
context_key = "menu_by_id"
def batch_load(self, keys):
- menus = Menu.objects.in_bulk(keys)
+ menus = Menu.objects.using(self.database_connection_name).in_bulk(keys)
return [menus.get(menu_id) for menu_id in keys]
@@ -16,7 +16,7 @@ class MenuItemByIdLoader(DataLoader):
context_key = "menuitem_by_id"
def batch_load(self, keys):
- menu_items = MenuItem.objects.in_bulk(keys)
+ menu_items = MenuItem.objects.using(self.database_connection_name).in_bulk(keys)
return [menu_items.get(menu_item_id) for menu_item_id in keys]
@@ -24,7 +24,9 @@ class MenuItemsByParentMenuLoader(DataLoader):
context_key = "menuitems_by_parent_menu"
def batch_load(self, keys):
- menu_items = MenuItem.objects.filter(menu_id__in=keys, level=0)
+ menu_items = MenuItem.objects.using(self.database_connection_name).filter(
+ menu_id__in=keys, level=0
+ )
items_map = defaultdict(list)
for menu_item in menu_items:
items_map[menu_item.menu_id].append(menu_item)
@@ -35,7 +37,9 @@ class MenuItemChildrenLoader(DataLoader):
context_key = "menuitem_children"
def batch_load(self, keys):
- menu_items = MenuItem.objects.filter(parent_id__in=keys)
+ menu_items = MenuItem.objects.using(self.database_connection_name).filter(
+ parent_id__in=keys
+ )
items_map = defaultdict(list)
for menu_item in menu_items:
items_map[menu_item.parent_id].append(menu_item)
diff --git a/saleor/graphql/order/dataloaders.py b/saleor/graphql/order/dataloaders.py
index 279ebe0a17..7fc2e3b30a 100644
--- a/saleor/graphql/order/dataloaders.py
+++ b/saleor/graphql/order/dataloaders.py
@@ -13,9 +13,11 @@ class OrderLinesByVariantIdAndChannelIdLoader(DataLoader):
def batch_load(self, keys):
channel_ids = [key[1] for key in keys]
variant_ids = [key[0] for key in keys]
- order_lines = OrderLine.objects.filter(
- order__channel_id__in=channel_ids, variant_id__in=variant_ids
- ).annotate(channel_id=F("order__channel_id"))
+ order_lines = (
+ OrderLine.objects.using(self.database_connection_name)
+ .filter(order__channel_id__in=channel_ids, variant_id__in=variant_ids)
+ .annotate(channel_id=F("order__channel_id"))
+ )
order_line_by_variant_and_channel_map = defaultdict(list)
for order_line in order_lines:
@@ -28,7 +30,7 @@ class OrderByIdLoader(DataLoader):
context_key = "order_by_id"
def batch_load(self, keys):
- orders = Order.objects.in_bulk(keys)
+ orders = Order.objects.using(self.database_connection_name).in_bulk(keys)
return [orders.get(order_id) for order_id in keys]
@@ -36,7 +38,9 @@ class OrdersByUserLoader(DataLoader):
context_key = "order_by_user"
def batch_load(self, keys):
- orders = Order.objects.filter(user_id__in=keys)
+ orders = Order.objects.using(self.database_connection_name).filter(
+ user_id__in=keys
+ )
orders_by_user_map = defaultdict(list)
for order in orders:
orders_by_user_map[order.user_id].append(order)
@@ -47,7 +51,9 @@ class OrderLineByIdLoader(DataLoader):
context_key = "orderline_by_id"
def batch_load(self, keys):
- order_lines = OrderLine.objects.in_bulk(keys)
+ order_lines = OrderLine.objects.using(self.database_connection_name).in_bulk(
+ keys
+ )
return [order_lines.get(line_id) for line_id in keys]
@@ -55,7 +61,11 @@ class OrderLinesByOrderIdLoader(DataLoader):
context_key = "orderlines_by_order"
def batch_load(self, keys):
- lines = OrderLine.objects.filter(order_id__in=keys).order_by("pk")
+ lines = (
+ OrderLine.objects.using(self.database_connection_name)
+ .filter(order_id__in=keys)
+ .order_by("pk")
+ )
line_map = defaultdict(list)
for line in lines.iterator():
line_map[line.order_id].append(line)
@@ -66,7 +76,11 @@ class OrderEventsByOrderIdLoader(DataLoader):
context_key = "orderevents_by_order"
def batch_load(self, keys):
- events = OrderEvent.objects.filter(order_id__in=keys).order_by("pk")
+ events = (
+ OrderEvent.objects.using(self.database_connection_name)
+ .filter(order_id__in=keys)
+ .order_by("pk")
+ )
events_map = defaultdict(list)
for event in events.iterator():
events_map[event.order_id].append(event)
@@ -77,7 +91,9 @@ class AllocationsByOrderLineIdLoader(DataLoader):
context_key = "allocations_by_orderline_id"
def batch_load(self, keys):
- allocations = Allocation.objects.filter(order_line__pk__in=keys)
+ allocations = Allocation.objects.using(self.database_connection_name).filter(
+ order_line__pk__in=keys
+ )
order_lines_to_allocations = defaultdict(list)
for allocation in allocations:
@@ -90,7 +106,11 @@ class FulfillmentsByOrderIdLoader(DataLoader):
context_key = "fulfillments_by_order"
def batch_load(self, keys):
- fulfillments = Fulfillment.objects.filter(order_id__in=keys).order_by("pk")
+ fulfillments = (
+ Fulfillment.objects.using(self.database_connection_name)
+ .filter(order_id__in=keys)
+ .order_by("pk")
+ )
fulfillments_map = defaultdict(list)
for fulfillment in fulfillments.iterator():
fulfillments_map[fulfillment.order_id].append(fulfillment)
@@ -101,5 +121,7 @@ class FulfillmentLinesByIdLoader(DataLoader):
context_key = "fulfillment_lines_by_id"
def batch_load(self, keys):
- fulfillment_lines = FulfillmentLine.objects.in_bulk(keys)
+ fulfillment_lines = FulfillmentLine.objects.using(
+ self.database_connection_name
+ ).in_bulk(keys)
return [fulfillment_lines.get(line_id) for line_id in keys]
diff --git a/saleor/graphql/page/dataloaders.py b/saleor/graphql/page/dataloaders.py
index a6e22d1da0..4f07471c0f 100644
--- a/saleor/graphql/page/dataloaders.py
+++ b/saleor/graphql/page/dataloaders.py
@@ -18,7 +18,7 @@ class PageByIdLoader(DataLoader):
context_key = "page_by_id"
def batch_load(self, keys):
- pages = Page.objects.in_bulk(keys)
+ pages = Page.objects.using(self.database_connection_name).in_bulk(keys)
return [pages.get(page_id) for page_id in keys]
@@ -26,7 +26,7 @@ class PageTypeByIdLoader(DataLoader):
context_key = "page_type_by_id"
def batch_load(self, keys):
- page_types = PageType.objects.in_bulk(keys)
+ page_types = PageType.objects.using(self.database_connection_name).in_bulk(keys)
return [page_types.get(page_type_id) for page_type_id in keys]
@@ -36,7 +36,9 @@ class PagesByPageTypeIdLoader(DataLoader):
context_key = "pages_by_pagetype"
def batch_load(self, keys):
- pages = Page.objects.filter(page_type_id__in=keys)
+ pages = Page.objects.using(self.database_connection_name).filter(
+ page_type_id__in=keys
+ )
pagetype_to_pages = defaultdict(list)
for page in pages:
@@ -53,9 +55,11 @@ class PageAttributesByPageTypeIdLoader(DataLoader):
def batch_load(self, keys):
requestor = get_user_or_app_from_context(self.context)
if requestor.is_active and requestor.has_perm(PagePermissions.MANAGE_PAGES):
- qs = AttributePage.objects.all()
+ qs = AttributePage.objects.using(self.database_connection_name).all()
else:
- qs = AttributePage.objects.filter(attribute__visible_in_storefront=True)
+ qs = AttributePage.objects.using(self.database_connection_name).filter(
+ attribute__visible_in_storefront=True
+ )
page_type_attribute_pairs = qs.filter(page_type_id__in=keys).values_list(
"page_type_id", "attribute_id"
@@ -90,9 +94,11 @@ class AttributePagesByPageTypeIdLoader(DataLoader):
def batch_load(self, keys):
requestor = get_user_or_app_from_context(self.context)
if requestor.is_active and requestor.has_perm(PagePermissions.MANAGE_PAGES):
- qs = AttributePage.objects.all()
+ qs = AttributePage.objects.using(self.database_connection_name).all()
else:
- qs = AttributePage.objects.filter(attribute__visible_in_storefront=True)
+ qs = AttributePage.objects.using(self.database_connection_name).filter(
+ attribute__visible_in_storefront=True
+ )
attribute_pages = qs.filter(page_type_id__in=keys)
pagetype_to_attributepages = defaultdict(list)
for attribute_page in attribute_pages:
@@ -108,11 +114,13 @@ class AssignedPageAttributesByPageIdLoader(DataLoader):
def batch_load(self, keys):
requestor = get_user_or_app_from_context(self.context)
if requestor.is_active and requestor.has_perm(PagePermissions.MANAGE_PAGES):
- qs = AssignedPageAttribute.objects.all()
+ qs = AssignedPageAttribute.objects.using(
+ self.database_connection_name
+ ).all()
else:
- qs = AssignedPageAttribute.objects.filter(
- assignment__attribute__visible_in_storefront=True
- )
+ qs = AssignedPageAttribute.objects.using(
+ self.database_connection_name
+ ).filter(assignment__attribute__visible_in_storefront=True)
assigned_page_attributes = qs.filter(page_id__in=keys)
page_to_assignedattributes = defaultdict(list)
for assigned_page_attribute in assigned_page_attributes:
@@ -126,9 +134,9 @@ class AttributeValuesByAssignedPageAttributeIdLoader(DataLoader):
context_key = "attributevalues_by_assigned_pageattribute"
def batch_load(self, keys):
- attribute_values = AssignedPageAttributeValue.objects.filter(
- assignment_id__in=keys
- )
+ attribute_values = AssignedPageAttributeValue.objects.using(
+ self.database_connection_name
+ ).filter(assignment_id__in=keys)
value_ids = [a.value_id for a in attribute_values]
def map_assignment_to_values(values):
diff --git a/saleor/graphql/plugins/dataloaders.py b/saleor/graphql/plugins/dataloaders.py
index 6c900c6203..5744ba9f6c 100644
--- a/saleor/graphql/plugins/dataloaders.py
+++ b/saleor/graphql/plugins/dataloaders.py
@@ -10,7 +10,9 @@ class EmailTemplatesByPluginConfigurationLoader(DataLoader):
context_key = "email_template_by_plugin_configuration"
def batch_load(self, keys):
- email_templates = EmailTemplate.objects.filter(plugin_configuration_id__in=keys)
+ email_templates = EmailTemplate.objects.using(
+ self.database_connection_name
+ ).filter(plugin_configuration_id__in=keys)
config_to_template = defaultdict(list)
for et in email_templates:
diff --git a/saleor/graphql/product/dataloaders/attributes.py b/saleor/graphql/product/dataloaders/attributes.py
index bb54e3c771..ff31355335 100644
--- a/saleor/graphql/product/dataloaders/attributes.py
+++ b/saleor/graphql/product/dataloaders/attributes.py
@@ -33,9 +33,11 @@ class BaseProductAttributesByProductTypeIdLoader(DataLoader):
if requestor.is_active and requestor.has_perm(
ProductPermissions.MANAGE_PRODUCTS
):
- qs = self.model_name.objects.all()
+ qs = self.model_name.objects.using(self.database_connection_name).all()
else:
- qs = self.model_name.objects.filter(attribute__visible_in_storefront=True)
+ qs = self.model_name.objects.using(self.database_connection_name).filter(
+ attribute__visible_in_storefront=True
+ )
product_type_attribute_pairs = qs.filter(product_type_id__in=keys).values_list(
"product_type_id", "attribute_id", *self.extra_fields
@@ -95,9 +97,11 @@ class AttributeProductsByProductTypeIdLoader(DataLoader):
if requestor.is_active and requestor.has_perm(
ProductPermissions.MANAGE_PRODUCTS
):
- qs = AttributeProduct.objects.all()
+ qs = AttributeProduct.objects.using(self.database_connection_name).all()
else:
- qs = AttributeProduct.objects.filter(attribute__visible_in_storefront=True)
+ qs = AttributeProduct.objects.using(self.database_connection_name).filter(
+ attribute__visible_in_storefront=True
+ )
attribute_products = qs.filter(product_type_id__in=keys)
producttype_to_attributeproducts = defaultdict(list)
for attribute_product in attribute_products:
@@ -115,9 +119,11 @@ class AttributeVariantsByProductTypeIdLoader(DataLoader):
if requestor.is_active and requestor.has_perm(
ProductPermissions.MANAGE_PRODUCTS
):
- qs = AttributeVariant.objects.all()
+ qs = AttributeVariant.objects.using(self.database_connection_name).all()
else:
- qs = AttributeVariant.objects.filter(attribute__visible_in_storefront=True)
+ qs = AttributeVariant.objects.using(self.database_connection_name).filter(
+ attribute__visible_in_storefront=True
+ )
attribute_variants = qs.filter(product_type_id__in=keys)
producttype_to_attributevariants = defaultdict(list)
for attribute_variant in attribute_variants:
@@ -135,11 +141,13 @@ class AssignedProductAttributesByProductIdLoader(DataLoader):
if requestor.is_active and requestor.has_perm(
ProductPermissions.MANAGE_PRODUCTS
):
- qs = AssignedProductAttribute.objects.all()
+ qs = AssignedProductAttribute.objects.using(
+ self.database_connection_name
+ ).all()
else:
- qs = AssignedProductAttribute.objects.filter(
- assignment__attribute__visible_in_storefront=True
- )
+ qs = AssignedProductAttribute.objects.using(
+ self.database_connection_name
+ ).filter(assignment__attribute__visible_in_storefront=True)
assigned_product_attributes = qs.filter(product_id__in=keys)
product_to_assignedproductattributes = defaultdict(list)
for assigned_product_attribute in assigned_product_attributes:
@@ -157,11 +165,13 @@ class AssignedVariantAttributesByProductVariantId(DataLoader):
if requestor.is_active and requestor.has_perm(
ProductPermissions.MANAGE_PRODUCTS
):
- qs = AssignedVariantAttribute.objects.all()
+ qs = AssignedVariantAttribute.objects.using(
+ self.database_connection_name
+ ).all()
else:
- qs = AssignedVariantAttribute.objects.filter(
- assignment__attribute__visible_in_storefront=True
- )
+ qs = AssignedVariantAttribute.objects.using(
+ self.database_connection_name
+ ).filter(assignment__attribute__visible_in_storefront=True)
assigned_variant_attributes = qs.filter(variant_id__in=keys).select_related(
"assignment__attribute"
)
@@ -177,9 +187,9 @@ class AttributeValuesByAssignedProductAttributeIdLoader(DataLoader):
context_key = "attributevalues_by_assignedproductattribute"
def batch_load(self, keys):
- attribute_values = AssignedProductAttributeValue.objects.filter(
- assignment_id__in=keys
- )
+ attribute_values = AssignedProductAttributeValue.objects.using(
+ self.database_connection_name
+ ).filter(assignment_id__in=keys)
value_ids = [a.value_id for a in attribute_values]
def map_assignment_to_values(values):
@@ -202,9 +212,9 @@ class AttributeValuesByAssignedVariantAttributeIdLoader(DataLoader):
context_key = "attributevalues_by_assignedvariantattribute"
def batch_load(self, keys):
- attribute_values = AssignedVariantAttributeValue.objects.filter(
- assignment_id__in=keys
- )
+ attribute_values = AssignedVariantAttributeValue.objects.using(
+ self.database_connection_name
+ ).filter(assignment_id__in=keys)
value_ids = [a.value_id for a in attribute_values]
def map_assignment_to_values(values):
diff --git a/saleor/graphql/product/dataloaders/products.py b/saleor/graphql/product/dataloaders/products.py
index 9321a7f10f..2dbbe1c604 100644
--- a/saleor/graphql/product/dataloaders/products.py
+++ b/saleor/graphql/product/dataloaders/products.py
@@ -27,7 +27,7 @@ class CategoryByIdLoader(DataLoader):
context_key = "category_by_id"
def batch_load(self, keys):
- categories = Category.objects.in_bulk(keys)
+ categories = Category.objects.using(self.database_connection_name).in_bulk(keys)
return [categories.get(category_id) for category_id in keys]
@@ -56,7 +56,9 @@ class ProductChannelListingByIdLoader(DataLoader[int, ProductChannelListing]):
context_key = "productchannelisting_by_id"
def batch_load(self, keys):
- product_channel_listings = ProductChannelListing.objects.in_bulk(keys)
+ product_channel_listings = ProductChannelListing.objects.using(
+ self.database_connection_name
+ ).in_bulk(keys)
return [product_channel_listings.get(key) for key in keys]
@@ -64,9 +66,9 @@ class ProductChannelListingByProductIdLoader(DataLoader[int, ProductChannelListi
context_key = "productchannelisting_by_product"
def batch_load(self, keys):
- product_channel_listings = ProductChannelListing.objects.filter(
- product_id__in=keys
- )
+ product_channel_listings = ProductChannelListing.objects.using(
+ self.database_connection_name
+ ).filter(product_id__in=keys)
product_id_variant_channel_listings_map = defaultdict(list)
for product_channel_listing in product_channel_listings:
product_id_variant_channel_listings_map[
@@ -111,9 +113,9 @@ class ProductChannelListingByProductIdAndChannelSlugLoader(
def batch_load_channel(
self, channel_slug: str, products_ids: Iterable[int]
) -> Iterable[Tuple[int, Optional[ProductChannelListing]]]:
- product_channel_listings = ProductChannelListing.objects.filter(
- channel__slug=channel_slug, product_id__in=products_ids
- )
+ product_channel_listings = ProductChannelListing.objects.using(
+ self.database_connection_name
+ ).filter(channel__slug=channel_slug, product_id__in=products_ids)
product_channel_listings_map: Dict[int, ProductChannelListing] = {}
for product_channel_listing in product_channel_listings.iterator():
@@ -131,7 +133,9 @@ class ProductTypeByIdLoader(DataLoader):
context_key = "product_type_by_id"
def batch_load(self, keys):
- product_types = ProductType.objects.in_bulk(keys)
+ product_types = ProductType.objects.using(
+ self.database_connection_name
+ ).in_bulk(keys)
return [product_types.get(product_type_id) for product_type_id in keys]
@@ -139,7 +143,9 @@ class MediaByProductIdLoader(DataLoader):
context_key = "media_by_product"
def batch_load(self, keys):
- media = ProductMedia.objects.filter(product_id__in=keys)
+ media = ProductMedia.objects.using(self.database_connection_name).filter(
+ product_id__in=keys
+ )
media_map = defaultdict(list)
for media_obj in media:
media_map[media_obj.product_id].append(media_obj)
@@ -150,7 +156,7 @@ class ImagesByProductIdLoader(DataLoader):
context_key = "images_by_product"
def batch_load(self, keys):
- images = ProductMedia.objects.filter(
+ images = ProductMedia.objects.using(self.database_connection_name).filter(
product_id__in=keys, type=ProductMediaTypes.IMAGE
)
images_map = defaultdict(list)
@@ -173,7 +179,9 @@ class ProductVariantsByProductIdLoader(DataLoader):
context_key = "productvariants_by_product"
def batch_load(self, keys):
- variants = ProductVariant.objects.filter(product_id__in=keys)
+ variants = ProductVariant.objects.using(self.database_connection_name).filter(
+ product_id__in=keys
+ )
variant_map = defaultdict(list)
variant_loader = ProductVariantByIdLoader(self.context)
for variant in variants.iterator():
@@ -189,8 +197,10 @@ class ProductVariantsByProductIdAndChannel(DataLoader):
product_ids, channel_slugs = zip(*keys)
variants_filter = self.get_variants_filter(product_ids, channel_slugs)
- variants = ProductVariant.objects.filter(**variants_filter).annotate(
- channel_slug=F("channel_listings__channel__slug")
+ variants = (
+ ProductVariant.objects.using(self.database_connection_name)
+ .filter(**variants_filter)
+ .annotate(channel_slug=F("channel_listings__channel__slug"))
)
variant_map = defaultdict(list)
for variant in variants.iterator():
@@ -226,7 +236,9 @@ class ProductVariantChannelListingByIdLoader(DataLoader):
context_key = "productvariantchannelisting_by_id"
def batch_load(self, keys):
- variants = ProductVariantChannelListing.objects.in_bulk(keys)
+ variants = ProductVariantChannelListing.objects.using(
+ self.database_connection_name
+ ).in_bulk(keys)
return [variants.get(key) for key in keys]
@@ -234,9 +246,11 @@ class VariantChannelListingByVariantIdLoader(DataLoader):
context_key = "productvariantchannelisting_by_productvariant"
def batch_load(self, keys):
- variant_channel_listings = ProductVariantChannelListing.objects.filter(
- variant_id__in=keys
- ).annotate_preorder_quantity_allocated()
+ variant_channel_listings = (
+ ProductVariantChannelListing.objects.using(self.database_connection_name)
+ .filter(variant_id__in=keys)
+ .annotate_preorder_quantity_allocated()
+ )
variant_id_variant_channel_listings_map = defaultdict(list)
for variant_channel_listing in variant_channel_listings:
@@ -286,9 +300,11 @@ class VariantChannelListingByVariantIdAndChannelLoader(
"variant_id__in": variant_ids,
"price_amount__isnull": False,
}
- variant_channel_listings = ProductVariantChannelListing.objects.filter(
- **filter
- ).annotate_preorder_quantity_allocated()
+ variant_channel_listings = (
+ ProductVariantChannelListing.objects.using(self.database_connection_name)
+ .filter(**filter)
+ .annotate_preorder_quantity_allocated()
+ )
variant_channel_listings_map: Dict[int, ProductVariantChannelListing] = {}
for variant_channel_listing in variant_channel_listings.iterator():
@@ -336,10 +352,10 @@ class VariantsChannelListingByProductIdAndChannelSlugLoader(
ProductIdAndChannelSlug, Optional[Iterable[ProductVariantChannelListing]]
] = defaultdict()
for channel_slug, product_ids in variant_channel_listing_by_channel.items():
- varaint_channel_listings = self.batch_load_channel(
+ variant_channel_listings = self.batch_load_channel(
channel_slug, product_ids
)
- for product_id, variants_channel_listing in varaint_channel_listings:
+ for product_id, variants_channel_listing in variant_channel_listings:
variant_channel_listing_by_product_and_channel[
(product_id, channel_slug)
] = variants_channel_listing
@@ -351,11 +367,15 @@ class VariantsChannelListingByProductIdAndChannelSlugLoader(
def batch_load_channel(
self, channel_slug: str, products_ids: Iterable[int]
) -> Iterable[Tuple[int, Optional[List[ProductVariantChannelListing]]]]:
- variants_channel_listings = ProductVariantChannelListing.objects.filter(
- channel__slug=channel_slug,
- variant__product_id__in=products_ids,
- price_amount__isnull=False,
- ).annotate(product_id=F("variant__product_id"))
+ variants_channel_listings = (
+ ProductVariantChannelListing.objects.using(self.database_connection_name)
+ .filter(
+ channel__slug=channel_slug,
+ variant__product_id__in=products_ids,
+ price_amount__isnull=False,
+ )
+ .annotate(product_id=F("variant__product_id"))
+ )
variants_channel_listings_map: Dict[
int, List[ProductVariantChannelListing]
@@ -375,7 +395,9 @@ class ProductMediaByIdLoader(DataLoader):
context_key = "product_media_by_id"
def batch_load(self, keys):
- product_media = ProductMedia.objects.in_bulk(keys)
+ product_media = ProductMedia.objects.using(
+ self.database_connection_name
+ ).in_bulk(keys)
return [product_media.get(product_media_id) for product_media_id in keys]
@@ -383,7 +405,11 @@ class ProductImageByIdLoader(DataLoader):
context_key = "product_image_by_id"
def batch_load(self, keys):
- images = ProductMedia.objects.filter(type=ProductMediaTypes.IMAGE).in_bulk(keys)
+ images = (
+ ProductMedia.objects.using(self.database_connection_name)
+ .filter(type=ProductMediaTypes.IMAGE)
+ .in_bulk(keys)
+ )
return [images.get(product_image_id) for product_image_id in keys]
@@ -391,7 +417,7 @@ class ProductImageByProductIdLoader(DataLoader):
context_key = "product_image_by_product_id"
def batch_load(self, keys):
- medias = ProductMedia.objects.filter(
+ medias = ProductMedia.objects.using(self.database_connection_name).filter(
type=ProductMediaTypes.IMAGE, product_id__in=keys
)
product_id_medias_map = defaultdict(list)
@@ -404,8 +430,10 @@ class MediaByProductVariantIdLoader(DataLoader):
context_key = "media_by_product_variant"
def batch_load(self, keys):
- variant_media = VariantMedia.objects.filter(variant_id__in=keys).values_list(
- "variant_id", "media_id"
+ variant_media = (
+ VariantMedia.objects.using(self.database_connection_name)
+ .filter(variant_id__in=keys)
+ .values_list("variant_id", "media_id")
)
variant_media_pairs = defaultdict(list)
@@ -430,9 +458,11 @@ class ImagesByProductVariantIdLoader(DataLoader):
context_key = "images_by_product_variant"
def batch_load(self, keys):
- variant_media = VariantMedia.objects.filter(
- variant_id__in=keys, media__type=ProductMediaTypes.IMAGE
- ).values_list("variant_id", "media_id")
+ variant_media = (
+ VariantMedia.objects.using(self.database_connection_name)
+ .filter(variant_id__in=keys, media__type=ProductMediaTypes.IMAGE)
+ .values_list("variant_id", "media_id")
+ )
variant_media_pairs = defaultdict(list)
for variant_id, media_id in variant_media:
@@ -456,8 +486,10 @@ class CollectionByIdLoader(DataLoader):
context_key = "collection_by_id"
def batch_load(self, keys):
- collections = Collection.objects.using(self.database_connection_name).in_bulk(
- keys
+ collections = (
+ Collection.objects.using(self.database_connection_name)
+ .using(self.database_connection_name)
+ .in_bulk(keys)
)
return [collections.get(collection_id) for collection_id in keys]
@@ -468,6 +500,7 @@ class CollectionsByProductIdLoader(DataLoader):
def batch_load(self, keys):
product_collection_pairs = list(
CollectionProduct.objects.using(self.database_connection_name)
+ .using(self.database_connection_name)
.filter(product_id__in=keys)
.order_by("id")
.values_list("product_id", "collection_id")
@@ -509,9 +542,11 @@ class ProductTypeByProductIdLoader(DataLoader):
def batch_load(self, keys):
def with_products(products):
product_ids = {p.id for p in products}
- product_types_map = ProductType.objects.filter(
- products__in=product_ids
- ).in_bulk()
+ product_types_map = (
+ ProductType.objects.using(self.database_connection_name)
+ .filter(products__in=product_ids)
+ .in_bulk()
+ )
return [product_types_map[product.product_type_id] for product in products]
return ProductByIdLoader(self.context).load_many(keys).then(with_products)
@@ -534,7 +569,9 @@ class CollectionChannelListingByIdLoader(DataLoader):
context_key = "collectionchannelisting_by_id"
def batch_load(self, keys):
- collections = CollectionChannelListing.objects.in_bulk(keys)
+ collections = CollectionChannelListing.objects.using(
+ self.database_connection_name
+ ).in_bulk(keys)
return [collections.get(key) for key in keys]
@@ -542,9 +579,9 @@ class CollectionChannelListingByCollectionIdLoader(DataLoader):
context_key = "collectionchannelisting_by_collection"
def batch_load(self, keys):
- collections_channel_listings = CollectionChannelListing.objects.filter(
- collection_id__in=keys
- )
+ collections_channel_listings = CollectionChannelListing.objects.using(
+ self.database_connection_name
+ ).filter(collection_id__in=keys)
collection_id_collection_channel_listings_map = defaultdict(list)
for collection_channel_listing in collections_channel_listings:
collection_id_collection_channel_listings_map[
@@ -562,9 +599,11 @@ class CollectionChannelListingByCollectionIdAndChannelSlugLoader(DataLoader):
def batch_load(self, keys):
collection_ids = [key[0] for key in keys]
channel_slugs = [key[1] for key in keys]
- collections_channel_listings = CollectionChannelListing.objects.filter(
- collection_id__in=collection_ids, channel__slug__in=channel_slugs
- ).annotate(channel_slug=F("channel__slug"))
+ collections_channel_listings = (
+ CollectionChannelListing.objects.using(self.database_connection_name)
+ .filter(collection_id__in=collection_ids, channel__slug__in=channel_slugs)
+ .annotate(channel_slug=F("channel__slug"))
+ )
collections_channel_listings_by_collection_and_channel_map = {}
for collections_channel_listing in collections_channel_listings:
key = (
@@ -584,7 +623,9 @@ class CategoryChildrenByCategoryIdLoader(DataLoader):
context_key = "categorychildren_by_category"
def batch_load(self, keys):
- categories = Category.objects.filter(parent__isnull=False)
+ categories = Category.objects.using(self.database_connection_name).filter(
+ parent__isnull=False
+ )
parent_to_children_mapping = defaultdict(list)
for category in categories:
parent_to_children_mapping[category.parent_id].append(category)
diff --git a/saleor/graphql/shipping/dataloaders.py b/saleor/graphql/shipping/dataloaders.py
index 78440fdf40..874ab11534 100644
--- a/saleor/graphql/shipping/dataloaders.py
+++ b/saleor/graphql/shipping/dataloaders.py
@@ -16,7 +16,9 @@ class ShippingMethodByIdLoader(DataLoader):
context_key = "shippingmethod_by_id"
def batch_load(self, keys):
- shipping_methods = ShippingMethod.objects.in_bulk(keys)
+ shipping_methods = ShippingMethod.objects.using(
+ self.database_connection_name
+ ).in_bulk(keys)
return [shipping_methods.get(shipping_method_id) for shipping_method_id in keys]
@@ -24,7 +26,9 @@ class ShippingZoneByIdLoader(DataLoader):
context_key = "shippingzone_by_id"
def batch_load(self, keys):
- shipping_zones = ShippingZone.objects.in_bulk(keys)
+ shipping_zones = ShippingZone.objects.using(
+ self.database_connection_name
+ ).in_bulk(keys)
return [shipping_zones.get(shipping_zone_id) for shipping_zone_id in keys]
@@ -32,7 +36,9 @@ class ShippingMethodsByShippingZoneIdLoader(DataLoader):
context_key = "shippingmethod_by_shippingzone"
def batch_load(self, keys):
- shipping_methods = ShippingMethod.objects.filter(shipping_zone_id__in=keys)
+ shipping_methods = ShippingMethod.objects.using(
+ self.database_connection_name
+ ).filter(shipping_zone_id__in=keys)
shipping_methods_by_shipping_zone_map = defaultdict(list)
for shipping_method in shipping_methods:
shipping_methods_by_shipping_zone_map[
@@ -48,9 +54,11 @@ class PostalCodeRulesByShippingMethodIdLoader(DataLoader):
context_key = "postal_code_rules_by_shipping_method"
def batch_load(self, keys):
- postal_code_rules = ShippingMethodPostalCodeRule.objects.filter(
- shipping_method_id__in=keys
- ).order_by("id")
+ postal_code_rules = (
+ ShippingMethodPostalCodeRule.objects.using(self.database_connection_name)
+ .filter(shipping_method_id__in=keys)
+ .order_by("id")
+ )
postal_code_rules_map = defaultdict(list)
for postal_code in postal_code_rules:
@@ -65,9 +73,11 @@ class ShippingMethodsByShippingZoneIdAndChannelSlugLoader(DataLoader):
context_key = "shippingmethod_by_shippingzone_and_channel"
def batch_load(self, keys):
- shipping_methods = ShippingMethod.objects.filter(
- shipping_zone_id__in=keys
- ).annotate(channel_slug=F("channel_listings__channel__slug"))
+ shipping_methods = (
+ ShippingMethod.objects.using(self.database_connection_name)
+ .filter(shipping_zone_id__in=keys)
+ .annotate(channel_slug=F("channel_listings__channel__slug"))
+ )
shipping_methods_by_shipping_zone_and_channel_map = defaultdict(list)
for shipping_method in shipping_methods:
@@ -85,9 +95,9 @@ class ShippingMethodChannelListingByShippingMethodIdLoader(DataLoader):
context_key = "shippingmethodchannellisting_by_shippingmethod"
def batch_load(self, keys):
- shipping_method_channel_listings = ShippingMethodChannelListing.objects.filter(
- shipping_method_id__in=keys
- )
+ shipping_method_channel_listings = ShippingMethodChannelListing.objects.using(
+ self.database_connection_name
+ ).filter(shipping_method_id__in=keys)
shipping_method_channel_listings_by_shipping_method_map = defaultdict(list)
for shipping_method_channel_listing in shipping_method_channel_listings:
shipping_method_channel_listings_by_shipping_method_map[
@@ -105,9 +115,11 @@ class ShippingMethodChannelListingByChannelSlugLoader(DataLoader):
context_key = "shippingmethodchannellisting_by_channel"
def batch_load(self, keys):
- shipping_method_channel_listings = ShippingMethodChannelListing.objects.filter(
- channel__slug__in=keys
- ).annotate(channel_slug=F("channel__slug"))
+ shipping_method_channel_listings = (
+ ShippingMethodChannelListing.objects.using(self.database_connection_name)
+ .filter(channel__slug__in=keys)
+ .annotate(channel_slug=F("channel__slug"))
+ )
shipping_method_channel_listings_by_channel_slug = defaultdict(list)
for shipping_method_channel_listing in shipping_method_channel_listings:
shipping_method_channel_listings_by_channel_slug[
@@ -151,9 +163,11 @@ class ChannelsByShippingZoneIdLoader(DataLoader):
def batch_load(self, keys):
from ..channel.dataloaders import ChannelByIdLoader
- channel_and_zone_is_pairs = Channel.objects.filter(
- shipping_zones__id__in=keys
- ).values_list("pk", "shipping_zones__id")
+ channel_and_zone_is_pairs = (
+ Channel.objects.using(self.database_connection_name)
+ .filter(shipping_zones__id__in=keys)
+ .values_list("pk", "shipping_zones__id")
+ )
shipping_zone_channel_map = defaultdict(list)
for channel_id, zone_id in channel_and_zone_is_pairs:
shipping_zone_channel_map[zone_id].append(channel_id)
diff --git a/saleor/graphql/warehouse/dataloaders.py b/saleor/graphql/warehouse/dataloaders.py
index 3d2bf94f8e..632f47bfe8 100644
--- a/saleor/graphql/warehouse/dataloaders.py
+++ b/saleor/graphql/warehouse/dataloaders.py
@@ -72,25 +72,37 @@ class AvailableQuantityByProductVariantIdCountryCodeAndChannelSlugLoader(
) -> Iterable[Tuple[int, int]]:
# get stocks only for warehouses assigned to the shipping zones
# that are available in the given channel
- stocks = Stock.objects.filter(product_variant_id__in=variant_ids)
+ stocks = Stock.objects.using(self.database_connection_name).filter(
+ product_variant_id__in=variant_ids
+ )
WarehouseShippingZone = Warehouse.shipping_zones.through # type: ignore
- warehouse_shipping_zones = WarehouseShippingZone.objects.all()
+ warehouse_shipping_zones = WarehouseShippingZone.objects.using(
+ self.database_connection_name
+ ).all()
additional_warehouse_filter = False
if country_code or channel_slug:
additional_warehouse_filter = True
if country_code:
- shipping_zones = ShippingZone.objects.filter(
- countries__contains=country_code
- ).values("pk")
+ shipping_zones = (
+ ShippingZone.objects.using(self.database_connection_name)
+ .filter(countries__contains=country_code)
+ .values("pk")
+ )
warehouse_shipping_zones = warehouse_shipping_zones.filter(
Exists(shipping_zones.filter(pk=OuterRef("shippingzone_id")))
)
if channel_slug:
ShippingZoneChannel = Channel.shipping_zones.through # type: ignore
- channels = Channel.objects.filter(slug=channel_slug).values("pk")
- shipping_zone_channels = ShippingZoneChannel.objects.filter(
- Exists(channels.filter(pk=OuterRef("channel_id")))
- ).values("shippingzone_id")
+ channels = (
+ Channel.objects.using(self.database_connection_name)
+ .filter(slug=channel_slug)
+ .values("pk")
+ )
+ shipping_zone_channels = (
+ ShippingZoneChannel.objects.using(self.database_connection_name)
+ .filter(Exists(channels.filter(pk=OuterRef("channel_id"))))
+ .values("shippingzone_id")
+ )
warehouse_shipping_zones = warehouse_shipping_zones.filter(
Exists(
shipping_zone_channels.filter(
@@ -113,7 +125,8 @@ class AvailableQuantityByProductVariantIdCountryCodeAndChannelSlugLoader(
# available_quantity annotated value incorrect thanks to how
# Django's ORM builds SQLs with annotations
reservations_qs = (
- Stock.objects.filter(product_variant_id__in=variant_ids)
+ Stock.objects.using(self.database_connection_name)
+ .filter(product_variant_id__in=variant_ids)
.annotate_reserved_quantity()
.values_list("id", "reserved_quantity")
)
@@ -210,7 +223,9 @@ class StocksWithAvailableQuantityByProductVariantIdCountryCodeAndChannelLoader(
channel_slug: Optional[str],
variant_ids: Iterable[int],
) -> Iterable[Tuple[int, List[Stock]]]:
- stocks = Stock.objects.filter(product_variant_id__in=variant_ids)
+ stocks = Stock.objects.using(self.database_connection_name).filter(
+ product_variant_id__in=variant_ids
+ )
if country_code:
stocks = stocks.filter(
warehouse__shipping_zones__countries__contains=country_code
@@ -273,16 +288,20 @@ class ActiveReservationsByCheckoutLineIdLoader(DataLoader):
def batch_load(self, keys):
reservations_by_checkout_line = defaultdict(list)
- queryset = Reservation.objects.filter(
- checkout_line_id__in=keys
- ).not_expired() # type: ignore
+ queryset = (
+ Reservation.objects.using(self.database_connection_name)
+ .filter(checkout_line_id__in=keys)
+ .not_expired()
+ ) # type: ignore
for reservation in queryset:
reservations_by_checkout_line[reservation.checkout_line_id].append(
reservation
)
- queryset = PreorderReservation.objects.filter(
- checkout_line_id__in=keys
- ).not_expired() # type: ignore
+ queryset = (
+ PreorderReservation.objects.using(self.database_connection_name)
+ .filter(checkout_line_id__in=keys)
+ .not_expired()
+ ) # type: ignore
for reservation in queryset:
reservations_by_checkout_line[reservation.checkout_line_id].append(
reservation
@@ -295,7 +314,8 @@ class PreorderQuantityReservedByVariantChannelListingIdLoader(DataLoader):
def batch_load(self, keys):
queryset = (
- ProductVariantChannelListing.objects.filter(id__in=keys)
+ ProductVariantChannelListing.objects.using(self.database_connection_name)
+ .filter(id__in=keys)
.annotate(
quantity_reserved=Coalesce(
Sum("preorder_reservations__quantity_reserved"),
@@ -316,5 +336,7 @@ class WarehouseByIdLoader(DataLoader):
context_key = "warehouse_by_id"
def batch_load(self, keys: Iterable[UUID]) -> List[Optional[Warehouse]]:
- warehouses = Warehouse.objects.in_bulk(keys)
+ warehouses = Warehouse.objects.using(self.database_connection_name).in_bulk(
+ keys
+ )
return [warehouses.get(warehouse_uuid) for warehouse_uuid in keys]
diff --git a/saleor/graphql/webhook/dataloaders.py b/saleor/graphql/webhook/dataloaders.py
index ff5c998b5d..253be01421 100644
--- a/saleor/graphql/webhook/dataloaders.py
+++ b/saleor/graphql/webhook/dataloaders.py
@@ -6,5 +6,7 @@ class PayloadByIdLoader(DataLoader):
context_key = "payload_by_id"
def batch_load(self, keys):
- payload = EventPayload.objects.in_bulk(keys)
+ payload = EventPayload.objects.using(self.database_connection_name).in_bulk(
+ keys
+ )
return [payload.get(payload_id).payload for payload_id in keys]
diff --git a/saleor/payment/dataloaders.py b/saleor/payment/dataloaders.py
index 9e9d1c1662..446ded528a 100644
--- a/saleor/payment/dataloaders.py
+++ b/saleor/payment/dataloaders.py
@@ -8,7 +8,11 @@ class PaymentsByOrderIdLoader(DataLoader):
context_key = "payments_by_order"
def batch_load(self, keys):
- payments = Payment.objects.filter(order_id__in=keys).order_by("pk")
+ payments = (
+ Payment.objects.using(self.database_connection_name)
+ .filter(order_id__in=keys)
+ .order_by("pk")
+ )
payment_map = defaultdict(list)
for payment in payments.iterator():
payment_map[payment.order_id].append(payment)