How to solve this query set? [duplicate]


I have a problem where I want to subtract two fields and then update a record, which would be cantidad - stock , and the latter would be the modified one, here the model:

class Pedido(models.Model):
    especialidad   = models.ForeignKey('Especialidad')
    fecha_entrega  = models.DateTimeField(auto_now_add=False)
    fecha_pedido   = models.DateTimeField(auto_now_add=True,null=True, blank=True)
    bodega         = models.ForeignKey(Bodega, null=True, blank=True, on_delete=models.CASCADE)
    articulo = ChainedForeignKey(
    cantidad       = models.CharField(max_length=999, blank=True)
    pendiente      = models.CharField(max_length=999,  null=True, blank=True)

    def __str__(self):
        return '{}'.format(self.articulo, self.cantidad) 

class Articulo(models.Model):
    cod_experto = models.CharField(max_length=999, primary_key=True, blank=True)
    nombre      = models.CharField(max_length=999, blank=True)
    descripcion = models.CharField(max_length=999, blank=True, null=True)
    info_bodega = models.ForeignKey(Bodega, null=True, blank=True, on_delete=models.CASCADE)
    stock       = models.CharField(max_length=999, blank=True)
    extmin      = models.CharField(max_length=999, blank=True, null=True)
    extmax      = models.CharField(max_length=999, blank=True, null=True)

    def __str__(self):
        return '{}'.format(self.nombre) 

And here we have the query set of :

def succes(request, cod_experto):
    articulo   = Articulo.objects.filter(id=cod_experto)
    for x in cod_experto:
        cant_articulo = Articulo.objects.filter(id=x.cod_experto)
        for z in cant_articulo:            
            total = z.cantidad - x.stock
            update = Articulo.objects.values('stock').filter(id=x.cod_experto).update(stock=total)
    return redirect(reverse('usuario:index.html', kwargs={'pk':}))

Maybe my logic with the models is not correct, if they manage to understand and formulate a better solution to this problem, it would be greatly appreciated.

asked by Demaro Create 13.02.2017 в 15:15

2 answers


In Django when you do a Filter, you get a list of elements. In this case it is better to use a get .

Also, in your models you have Order.Quantity as a CharField type, should not be an int ?. The same for Articulo.stock.

Now, what is cod_experto that you receive in succes? a list?

Could you better explain what you want to do in your view succes? (What do you get in cod_expert ?, a list?)

answered by 13.02.2017 в 15:46

Good, seeing the code of the view, I would say that you formulate it like this:

    def succes(request, cod_experto):

    # Acá esta de mas, porque al ser clave primaria solo va a encontrar uno así que seria un get no filter  

    articulo   = Articulo.objects.get(cod_experto=cod_experto)

    # este for estaría de mas o te falto nombrar que viene mas de un código? 
    #         for x in cod_experto:
     #       cant_articulo = Articulo.objects.get(cod=x.cod_experto)

    # este for estaría de mas tambien 

#for z in cant_articulo:  
     # por ultimo el total nuevo es el calculo, donde te falta decir cuanto se pidio, osea pasar por args la cantidad pedida o tomar del formulario esa misma cantidad#  

                total = articulo.stock- pedido.cantidad
                update = Articulo.objects.values('stock').filter(cod_experto=cod_experto).update(stock=total)
        return redirect(reverse('usuario:index.html', kwargs={'pk':}))

I hope it serves you, regards

answered by 13.02.2017 в 16:09