'How can I install new product logic in Django?

In Django, when the product is newly added, I want the new product to be named for about 1 week and appear on the new products page. How can I build the logic of this?

model.py

class Product(models.Model):
name = models.CharField(max_length=100)
category = models.ForeignKey(Category, on_delete=models.DO_NOTHING)
main_image = models.ImageField(upload_to='static/product_images/%Y/%m/%d/')
detail = models.TextField()
keywords = models.CharField(max_length=50)
description = models.CharField(max_length=1000)
price = models.FloatField()
brand = models.CharField(max_length=50, default='Markon', verbose_name='Brand (Default: Markon)')
sale = models.IntegerField(blank=True, null=True, verbose_name="Sale (%)")
bestseller = models.BooleanField(default=False)
amount = models.IntegerField(blank=True, null=True)
available = models.BooleanField(default=True)
stock = models.BooleanField(default=True)
date_created = models.DateTimeField(auto_now_add=True)

def __str__(self):
    return self.name

@property
def discount(self):
    dis = float(self.price - (self.price * self.sale) / 100)
    ln = ''
    if len(str(dis)) > 3:
        for i in str(dis):
            ln += i
            dis = float(ln)
            if len(ln) > 3:
                break
    return dis


Solution 1:[1]

In your view for the new products page, filter queryset by date_created field:

date_week_ago = datetime.now() - timedelta(days=7)
queryset = Product.objects.filter(date_created__gte=date_week_ago)

This way you will get only Products created within last 7 days

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bartosz Stasiak