Building the coupon models

Let's start by creating the Coupon model. Edit the models.py file of the coupons application and add the following code to it:

from django.db import modelsfrom django.core.validators import MinValueValidator, \                                   MaxValueValidatorclass Coupon(models.Model):    code = models.CharField(max_length=50,                            unique=True)    valid_from = models.DateTimeField()    valid_to = models.DateTimeField()    discount = models.IntegerField(                   validators=[MinValueValidator(0),                               MaxValueValidator(100)])    active = models.BooleanField()    def __str__(self):        return self.code

This is the model that we are going to use to store coupons. The Coupon model contains the following fields:

  • code: The code that users have to enter in order to apply the coupon to their ...

Get Django 2 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.