-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathmodels.py
More file actions
38 lines (32 loc) · 1.34 KB
/
models.py
File metadata and controls
38 lines (32 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Create your models here.
from django.db import models # type: ignore
from users.models import User
class Product(models.Model):
CATEGORY_CHOICES = [
('FR', 'Fruits'),
('VG', 'Vegetables'),
('DA', 'Dairy'),
('MT', 'Meat'),
('GR', 'Grains'),
]
name = models.CharField(max_length=100)
description = models.TextField()
price = models.DecimalField(max_digits=10, decimal_places=2)
category = models.CharField(max_length=2, choices=CATEGORY_CHOICES)
stock = models.PositiveIntegerField(default=0)
image = models.ImageField(upload_to='products/', null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
class Cart(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class CartItem(models.Model):
cart = models.ForeignKey(Cart, related_name='items', on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
quantity = models.PositiveIntegerField(default=1)
added_at = models.DateTimeField(auto_now_add=True)
def total_price(self):
return self.product.price * self.quantity