We are going to store the coupon that was applied to each order. First, we need to modify the Order model to store the related Coupon object, if there is any.
Edit the models.py file of the orders application and add the following imports to it:
from decimal import Decimalfrom django.core.validators import MinValueValidator, \ MaxValueValidatorfrom coupons.models import Coupon
Then, add the following fields to the Order model:
class Order(models.Model): # ... coupon = models.ForeignKey(Coupon, related_name='orders', null=True, blank=True, on_delete=models.SET_NULL) discount = models.IntegerField(default=0, validators=[MinValueValidator(0), MaxValueValidator(100)])
These fields allow us to store an optional coupon ...