Answer the question
In order to leave comments, you need to log in
Django, how to make a discount for a product?
there is a product model in which there is a price field, you need to implement a discount. I made a similar field in the product model for a discount, but now how can I make it so that when a discount is set in the admin panel, it is automatically calculated and the amount of the discounted product already gets into the basket?
class Product(models. Model):
category = models.ForeignKey(Category, related_name='product', verbose_name='Категория')
name = models.CharField(max_length=200, db_index=True, verbose_name='Название')
slug = models.SlugField(max_length=200, db_index=True, verbose_name='Транслит')
image = models.ImageField(upload_to='products/%y/%m/%d/', blank=True, verbose_name='Изображение товара')
description = models.TextField(blank=True, verbose_name='Описание')
price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='Цена')
discount = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True, verbose_name='Скидка')
Answer the question
In order to leave comments, you need to log in
You can add a percentage discount field in the product model like this:
In the same model, add a method for obtaining this discount:
def get_sale(self):
'''Расчитать стоимость со скидкой'''
price = int(self.price * (100 - self.sale) / 100)
return price
@property
def get_final_price:
return self.price - self.price * self.discount
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question