-
Notifications
You must be signed in to change notification settings - Fork 44
/
models.py
67 lines (49 loc) · 1.74 KB
/
models.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from django.conf import settings
from django.db import models
from django.shortcuts import reverse
CATEGORY = (
('S', 'Shirt'),
('SP', 'Sport Wear'),
('OW', 'Out Wear')
)
LABEL = (
('N', 'New'),
('BS', 'Best Seller')
)
class Item(models.Model) :
item_name = models.CharField(max_length=100)
price = models.FloatField()
discount_price = models.FloatField(blank=True, null=True)
category = models.CharField(choices=CATEGORY, max_length=2)
label = models.CharField(choices=LABEL, max_length=2)
description = models.TextField()
def __str__(self):
return self.item_name
def get_absolute_url(self):
return reverse("core:product", kwargs={
"pk" : self.pk
})
def get_add_to_cart_url(self) :
return reverse("core:add-to-cart", kwargs={
"pk" : self.pk
})
def get_remove_from_cart_url(self) :
return reverse("core:remove-from-cart", kwargs={
"pk" : self.pk
})
class OrderItem(models.Model) :
user = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
ordered = models.BooleanField(default=False)
item = models.ForeignKey(Item, on_delete=models.CASCADE)
quantity = models.IntegerField(default=1)
def __str__(self):
return f"{self.quantity} of {self.item.item_name}"
class Order(models.Model) :
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
items = models.ManyToManyField(OrderItem)
start_date = models.DateTimeField(auto_now_add=True)
ordered_date = models.DateTimeField()
ordered = models.BooleanField(default=False)
def __str__(self):
return self.user.username