-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
43 lines (35 loc) · 1.32 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
from pyexpat import model
from tabnanny import verbose
from django.db import models
from django.contrib.auth import get_user_model
from tienda.models import Producto
from django.db.models import F, Sum, FloatField
User = get_user_model()
class Pedido(models.Model):
user = models.ForeignKey(User, on_delete= models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.id
@property
def total(self):
return self.lineapedido_set.aggregate(
total=Sum(F("precio")*F("cantidad"),output_field = FloatField())
)["total"]
class Meta:
db_table = 'pedidos'
verbose_name = 'pedido'
verbose_name_plural = 'pedidos'
ordering = ['id']
class LineaPedido(models.Model):
user = models.ForeignKey(User, on_delete= models.CASCADE)
producto_id = models.ForeignKey(Producto, on_delete= models.CASCADE)
pedido_id = models.ForeignKey(Pedido, on_delete= models.CASCADE)
cantidad = models.IntegerField(default=1)
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return f'{self.cantidad} Unidades de {self.producto_id.nombre}'
class Meta:
db_table = 'lineapedidos'
verbose_name = 'lineapedido'
verbose_name_plural = 'lineapedidos'
ordering = ['id']