79192297

Date: 2024-11-15 12:09:04
Score: 1.5
Natty:
Report link

Following @sudip-parajuli's suggestions I modified my PromoCode serializer as below:

class PromoCodeSerializer(serializers.ModelSerializer):
    code = serializers.CharField(allow_blank=True)
    class Meta:
        model = PromoCode

I also overrode the validate method of the OrderSerializer to remove promo_code data when the code value is empty:

def validate(self, data: dict):
    promo_code_data: dict = data.get("promo_code", None)
    if promo_code_data and promo_code_data.get("code"):
        return data
    else:
        data.pop("promo_code", None)
        return data

Solved my problem.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @sudip-parajuli's
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: devKOfori