I have a model called Pedido
and another called LineasEstadoPedido
that saves the states through which the order goes through.
I need to get the last status of each order with a query and then use the orders that are in a specific status.
I am storing the states with a choice:
class Pedido(models.Model):
id = models.BigAutoField(primary_key = True)
f_ped = models.DateTimeField(auto_now_add = True)
p_prod = models.DecimalField(max_digits=14,decimal_places=2) #precio producto
p_envio = models.DecimalField(max_digits=6,decimal_places=2)
cp_nom_producto = models.CharField(max_length = 40)
cp_talla = models.CharField(max_length = 5)
cp_marca = models.CharField(max_length = 20)
cp_nombre_completo = models.CharField(max_length=50,default=None) #nombre del comprador
producto = models.ForeignKey(Producto)
perfil = models.ForeignKey(Perfil, related_name='perfil_comprador')
vendedor = models.ForeignKey(Perfil, related_name='perfil_vendedor', blank=True, null=True)
cp_total = models.DecimalField(max_digits=14,decimal_places=2, blank=True, null=True)
class LineasEstadoPedido(models.Model):
id = models.BigAutoField(primary_key = True)
pedido = models.ForeignKey(Pedido,on_delete=models.CASCADE)
estado = models.IntegerField(choices = CHOICES_ESTADO_PEDIDO)
fecha_creacion = models.DateTimeField(auto_now_add=True)
choice:
CHOICES_ESTADO_PEDIDO = (
(1,'Pendiente de pago'),
(2,'Pagado'),
(3,'Error de Pago'),
(4,'Pendiente recogida'),
(5,'Enviado'),
(6,'Entregado'),
(7,'No recogido'),
(8,'Error envio'),
(9,'Incidencia'), # Estado marcado por comprador para solicitar devolución de pago
(10,'Completado'),
)
Right now I'm trying something like this:
for p in Pedido.objects.all():
if p.lineasestadopedido.all().last() == 9:
return p
I am using Python 3.5 and Django 1.11 .
you almost had it
This is returns all orders that have status 9
To add an attribute of a list in a single line you can do it like this: