0

I would like to know how to calculate and print the date in Django. Attached below is model.py. I need to calculate and print out how many days are left based on the date_date of the registered product table. I'd appreciate it if you could tell me how.

class Product(models.Model):
    product_code = models.AutoField(primary_key=True)
    username = models.ForeignKey(Member, on_delete=models.CASCADE, db_column='username')
    category_code = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True, related_name='products')
    name = models.CharField(max_length=200, db_index=True)
    slug = models.SlugField(max_length=200, db_index=True, unique=False, allow_unicode=True)
    image = models.ImageField(upload_to='products/%Y/%m/%d', blank=True)
    benefit = models.TextField()
    detail = models.TextField()
    target_price = models.IntegerField()
    start_date = models.DateField()
    due_date = models.DateField()
gpdnjs12
  • 1
  • 1
  • By date_date I assume you mean due_date? You should be able calculate the difference between two dates, https://stackoverflow.com/questions/151199/how-to-calculate-number-of-days-between-two-given-dates – User1010 Jul 11 '21 at 17:18

1 Answers1

0

Django datefield returns python datetime.date object. All you need to do is to subtract one datefield from another

Example:

date1 = date(1970,1,1)
date2 = date.today()
result = date2 - date1

The result will be timedelta object which you can use for your purposes