Home > Software design >  Django Models to calculate discount
Django Models to calculate discount

Time:09-22

I am doing an ecommerce project as my first django project and I am having some trouble on how to calculate my actual price and my discount price if there is a discount price so what I want to do is that if the admin adds a discount to a product I want to add a value to a field inside a model called discount price and in that model I want to calculate how much discount percentage the admin has put in and what the discounted price would be after applying the discount percentage

ps: English is my second language and I'm sorry if you were not able to understand

tldr : I want to calculate the price and the discount percentage and make the value it to another field in the model called discount price

this is my models for the product and discounts(please point out how I should improve and any of my mistakes)

    from django_extensions.db.fields import AutoSlugField
from django.core.validators import MinValueValidator, MaxValueValidator
# Create your models here.
class Products(models.Model):
    product_name   = models.CharField(max_length=200, unique=True)
    slug           = AutoSlugField(populate_from=['product_name'], unique=True)
    description    = models.TextField(max_length=500, blank=True)
    price          = models.IntegerField(validators = [MinValueValidator(0)])
    discount_price = models.IntegerField(validators = [MinValueValidator(0)],null=True)
    image1         = models.ImageField(upload_to='photos/Products')
    image2         = models.ImageField(upload_to='photos/Products')
    image3         = models.ImageField(upload_to= 'photos/Products')
    image4         = models.ImageField(upload_to= 'photos/Products')
    stock          = models.IntegerField(validators=[MinValueValidator(0)])
    Is_available   = models.BooleanField(default=True)
    category       = models.ForeignKey(Category, on_delete=models.CASCADE)
    created_date   = models.DateTimeField(auto_now_add=True)
    modified_date  = models.DateTimeField(auto_now=True)
class Meta:
        verbose_name = 'products'
        verbose_name_plural = 'products'
def get_url(self):
return reverse('product_page',args=[self.category.slug, self.slug])
def __str__(self):
return self.product_name

class OfferProduct(models.Model):
    product = models.OneToOneField(Products, related_name='category_offers', on_delete=models.CASCADE)
    discount = models.IntegerField(validators=[MinValueValidator(0),MaxValueValidator(99)],null=True,default = 0)
    is_active = models.BooleanField(default =True)
class Meta:
        verbose_name = 'Offer Product'
        verbose_name_plural = 'Offer Products'
def __str__(self):
return self.product.product_name

CodePudding user response:

========= model.py =================

class ProductModel(models.Model):
    name= models.CharField(max_length=300)
    og_price = models.IntegerField()
    discount = models.IntegerField()
    discounted_price = models.IntegerField(null=True)
    sell_price = models.IntegerField(null=True)
    
    created_at = models.DateField(auto_now_add=True)

    @property
    def discounted_price(self):
        return ((self.og_price)*(self.discount))/100

    @property
    def sell_price(self):
        return (self.og_price)-(self.discounted_price)

=========== Output ============

enter image description here

CodePudding user response:

example : price : 1000, discount: 100 so the percentage is 10%

you can add a property in the OfferProduct:

@property
   def discount_percentage(self):
       return (100 * discount)/product.price #add your logic
  • Related