fix(views): optimize database queries to solve N+1 problems 🔨🐛

This commit is contained in:
2025-07-25 23:22:35 +05:00
parent 118ad52c71
commit 4aae0064ca
5 changed files with 39 additions and 29 deletions

View File

@@ -36,9 +36,9 @@ class DeviceSerializer(serializers.ModelSerializer):
def get_pending_payment_id(self, obj):
unpaid_payment = (
Payment.objects.filter(devices=obj, paid=False)
.order_by("-created_at")
.first()
obj.unpaid_payments[0]
if hasattr(obj, "unpaid_payments") and obj.unpaid_payments
else None
)
return unpaid_payment.id if unpaid_payment else None
@@ -63,9 +63,9 @@ class AdminDeviceSerializer(serializers.ModelSerializer):
def get_pending_payment_id(self, obj):
unpaid_payment = (
Payment.objects.filter(devices=obj, paid=False)
.order_by("-created_at")
.first()
obj.unpaid_payments[0]
if hasattr(obj, "unpaid_payments") and obj.unpaid_payments
else None
)
return unpaid_payment.id if unpaid_payment else None