freebie/api/models.py

47 lines
No EOL
1.2 KiB
Python

from django.db import models
class Category(models.Model):
name = models.CharField(max_length=100)
slug = models.SlugField(unique=True)
class Meta:
verbose_name = 'Category'
verbose_name_plural = 'Categories'
def __str__(self):
return self.name
class Product(models.Model):
category = models.ForeignKey(
Category,
on_delete=models.CASCADE,
related_name='products',
)
name = models.CharField(max_length=200)
image = models.ImageField(upload_to='products/')
rating = models.DecimalField(max_digits=2, decimal_places=1)
price = models.DecimalField(max_digits=10, decimal_places=2)
old_price = models.DecimalField(
max_digits=10,
decimal_places=2,
null=True,
blank=True,
)
@property
def discount_percent(self):
if self.old_price and self.old_price > self.price:
return round(
(self.old_price - self.price) / self.old_price * 100
)
return None
class Meta:
verbose_name = 'Product'
verbose_name_plural = 'Products'
def __str__(self):
return self.name