#13751·saleor

VoucherInfoByVoucherCodeLoader dataloader uses prefetch_related

Author: maarcingebalaCreated Aug 21, 2023Updated Aug 1, 2026
Labelsperformancehelp wanted

While preparing a fix for Avatax logic, we have to apply some changes to vouchers and their dataloaders. We introduce a dataloader for fetching VoucherInfo, the data class that stores voucher object and primary keys of all objects that the voucher is applicable. The calculation logic for the discount amount is embedded in the Voucher model. It assumes that voucher.channel_listings is prefetched and calls voucher.channel_listings.all(). It causes additional DB queries for any non-prefetched voucher. Temporarily we added prefetch_related("channel_listings") to VoucherInfoByVoucherCodeLoader to reduce the number of DB queries, but we need to change this approach and introduce a separate dataloader that will fetch the voucher's channel listings data. This will also require rebuilding how we currently handle vouchers (calculations are done in the model's logic). Extracting logic from the Voucher model is the best way to do that, and extending it to accept already fetched channel_listings.