@@ -567,12 +567,12 @@ class OrderHistorySerializer(serializers.ModelSerializer):
567567 @extend_schema_field (serializers .ListField )
568568 def get_titles (self , instance ):
569569 titles = []
570- # Access prefetched lines without method calls to avoid N+1
571- lines = getattr (instance , ' _prefetched_objects_cache' , {}).get (' lines' , [])
570+ # Access prefetched lines without method calls to avoid N+1
571+ lines = getattr (instance , " _prefetched_objects_cache" , {}).get (" lines" , [])
572572 if not lines :
573573 # Fallback if not prefetched
574574 lines = list (instance .lines .all ())
575-
575+
576576 product_ids = [line .product_version .field_dict ["id" ] for line in lines ]
577577 products_by_id = models .Product .all_objects .in_bulk (product_ids )
578578
@@ -710,12 +710,16 @@ def to_representation(self, instance):
710710 raise AttributeError # noqa: TRY004
711711
712712 # Access prefetched transactions without method calls to avoid N+1
713- transactions = getattr (instance , '_prefetched_objects_cache' , {}).get ('transactions' , [])
713+ transactions = getattr (instance , "_prefetched_objects_cache" , {}).get (
714+ "transactions" , []
715+ )
714716 if not transactions :
715- # Fallback if not prefetched
717+ # Fallback if not prefetched
716718 transactions = list (instance .transactions .all ())
717-
718- transaction = max (transactions , key = lambda t : t .created_on ) if transactions else None
719+
720+ transaction = (
721+ max (transactions , key = lambda t : t .created_on ) if transactions else None
722+ )
719723
720724 return transaction # noqa: RET504
721725
@@ -833,11 +837,13 @@ class Meta:
833837class TransactionLineSerializer (serializers .BaseSerializer ):
834838 def to_representation (self , instance ):
835839 # Access prefetched discounts without method calls to avoid N+1
836- discounts = getattr (instance .order , '_prefetched_objects_cache' , {}).get ('discounts' , [])
840+ discounts = getattr (instance .order , "_prefetched_objects_cache" , {}).get (
841+ "discounts" , []
842+ )
837843 if not discounts :
838844 # Fallback if not prefetched
839845 discounts = list (instance .order .discounts .all ())
840-
846+
841847 coupon_redemption = discounts [0 ] if discounts else None
842848 discount = 0.0
843849
@@ -909,11 +915,13 @@ def get_order(self, instance):
909915 def get_coupon (self , instance ):
910916 """Get discount code from the discount redemption if available"""
911917 # Access prefetched discounts without method calls to avoid N+1
912- discounts = getattr (instance , '_prefetched_objects_cache' , {}).get ('discounts' , [])
918+ discounts = getattr (instance , "_prefetched_objects_cache" , {}).get (
919+ "discounts" , []
920+ )
913921 if not discounts :
914922 # Fallback if not prefetched
915923 discounts = list (instance .discounts .all ())
916-
924+
917925 coupon_redemption = discounts [0 ] if discounts else None
918926 if not coupon_redemption :
919927 return None
0 commit comments